String类中常用的方法

  1. 构造方法:
public String():空构造
public String(String original):把字符串常量值转成字符串
public String(byte[] bytes):把字节数组转成字符串	
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  1. 数组和字符串互转:
 //把一个字符串转换成字节数组:
        byte[] bytes = "abc".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
//把 字节数组准换成字符串:

        String string = new String(bytes);
        System.out.println(string);
 //可以把字节数组转换成字符串
        String s1 = new String(new byte[]{99, 100, 101});
        String s3 = new String(new byte[]{99, 100, 101}, 0, 2);//某一段转换为字符串;
//把字符数组转换成字符串
        String s4 = new String(new char[]{'a', 'b', 'c'});
        String s5 = new String(new char[]{'a', 'b', 'c'},2,1);//字符数组某一段转化为字符串;

 //1.把一个字符数组转换成字符串
	//1.1
        char[] chars = {'你', '们'};
        String s3 = new String(chars, 0, 1);//把一个字符数组根据指定索引,转换成字符串
        System.out.println(s3);
	//1.2
        String s4 = String.valueOf(chars, 0, 1);//把一个字符数组根据指定索引,转换成字符串
        System.out.println(s4);

 // 2.把一个int类型转换成字符串类型
        int num = 100;  //  "100"
        boolean flag = true;//"true"
        
        //2.1.拼空串
        String str = num + "";
        String s = flag + "";
        System.out.println(str + 100);
        System.out.println(s + "true");

        //2.2.String 类里面有一个静态方法,可以把很多种类型转换成字符串类型
        String s1 = String.valueOf(100);
        String s2 = String.valueOf(3.14);
   
 String s5 = "aaa" + "bbb" + "cccc" + "dddd";

//3. 利用concat直接可以连接字符串;
String concat = "aaa".concat("bbb").concat("cccc").concat("dddd");
        System.out.println(concat);
        
  1. 根据运行时数据区的不同,进而对象地址变化:
 		String s1 = "Hello";  //0x001
        String s2 = "Hello";  //0x001
  		//因为创建s2时会在字符串找会否有相同的值,若不同,则重新在堆开辟内存,对象地址也不同;
        String s3 = "Hel" + "lo"; //0x001
        String s4 = "Hel" + new String("lo"); //0x002
        String s5 = new String("Hello");  // 0x003
        String s6 = s5.intern(); //0x001
        s5中的内容跟s1内容一样,所以s6和s1地址相等;
        String s7 = "H";//0x007
        String s8 = "ello";//0x008
        String s9 = s7 + s8; //0x009
        //s7和s8会创建新的对象,所以s1和s9的地址也不相同;

        System.out.println(s1 == s2); //true
        System.out.println(s1 == s3); //true
        System.out.println(s1 == s4);  //false
        System.out.println(s1 == s9);  //false
        System.out.println(s4 == s5); //false
        System.out.println(s1 == s6); //true
  1. 字符串中常用的方法
        //与判断相关的一些方法
        boolean b = "abc".equals("ABC"); //判断两个字符串的内容是否相同,区分大写

        //不区分大小的比较
        boolean b1 = "abc".equalsIgnoreCase("ABc");
        System.out.println(b1);

        //判断一个字符串以什么开头
        boolean startsWith = "王思聪".startsWith("王");
        System.out.println(startsWith);

        //判断一个方法是否某字符串结尾
        boolean b2 = "王思聪".endsWith("聪");
        System.out.println(b2);


        //判断一个字符串是不是空串
        boolean b3 = "".isEmpty();

        boolean b4 = "".length() == 0;
        String str="abc";

        //判断一个字符串,是否包含另一个字符串

        boolean b5 = "往后余生,洗衣是你,做饭是你,带娃也是你".contains("余生2");
        System.out.println(b5);

  1. 与获取相关的一些方法
        //获取字符串的长度
        int length = "abc".length();
        
        //根据索引获取字符串中的某个字符
        String s = "abcdefg";
        char ch = s.charAt(s.length()-1);
        System.out.println(ch);

        //indexOf(),获取这个字符,第一次在字符串出现的索引,从前往后检索
        int index = "abcdefgafasdfljiodfasdfasfd".indexOf('g');
        System.out.println(index);

        //返回 -1 代表没有找到
        int index2 = "abcdefgafasdfljiodfasdfasfd".indexOf("f2");
        System.out.println(index2);

        
        //lastIndexOf() 从后往前检索,该字符第一次在字符串中出现的索引
        int last = "asdfasdfasdfefadfeafassdf".lastIndexOf('a');
        System.out.println(last);

        //判断一个字符,在该字符串中只出现过一次
        String str="asfarsdfridfaerfadfeadfeadfeasdfe";
        if(str.indexOf('i')==str.lastIndexOf('i')){
            System.out.println(true);
        }
        
		// public int indexOf (String str,int fromIndex):
        // 返回指定字符串在此字符串中从指定位置后第一次出现后,再次出现的索			 引。
        String str = "bcdefadasfaewwfaaaa";
        int index = str.indexOf('a', str.indexOf('a') + 1);
        System.out.println(index);

		//从指定索引 开始 截取后面所有的内容返回一个新的字符串
        String s = "好好学习,天天向上".substring(2);
        String s = "好好学习,天天向上".substring(5,9);//含头不含尾
        String s1 = "好好学习,天天向上";
     String s = s1.substring(s1.indexOf('天'), s1.indexOf('向') + 1);
     System.out.println(s);

	 //遍历字符串
     String str="asdfasdffw3fasfalksfdioeasddsfeafsf";
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            System.out.println(c);
        }

 //大小写字母的转换
        String s = "abc".toUpperCase(); //转大写
        String s1 = "ABC".toLowerCase();//转小写
        
        String str = "你好";  //utf-8占三个字节; gbk占两个字节;
        byte[] bytes = str.getBytes();//转成字节数组;
        //从字节数字中取出"你"
        String s = new String(bytes, 0, 3);//从开始索引到终止索引,从字节数组获取字符串;
        System.out.println(s);
        //输出字符串;


 //比较两个字符串的内容是否相同
        boolean b = "abc".equals("abc");
        System.out.println(b);


        //1.按照字典ASCII码顺序比,比差值;通过字典ASCII码顺序比不出来,就用长度比;
        int num = "abc".compareTo("abcdef");
		  System.out.println(num);
		  
       //2. public int compareToIgnoreCase (String str)跟上面一样 只是忽略大小写的比较
        int n = "abc".compareToIgnoreCase("aBC");
        System.out.println(n);


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值