目       录

1.判断功能的方法

2.转换功能的方法

3.获取功能的方法

4.替换功能的方法

5.去除字符串两空格的方法

6.按字典顺序比较两个字符串


1.判断功能的方法 

  • public boolean equals(Object obj):                         比较字符串的内容是否相同,区分大小写
  • public boolean equalsIgnoreCase(String str):        比较字符串的内容是否相同,忽略大小写
  • public boolean contains(String str):                        判断字符串中是否包含传递进来的字符串
  • public boolean startsWith(String str):                     判断字符串是否以传递进来的字符串开头
  • public boolean endsWith(String str):                      判断字符串是否以传递进来的字符串结尾
  • public boolean isEmpty():                                       判断字符串的内容是否为空串
public class Test {
    public static void main(String[] args) {

        boolean b = "abc".equals("ABC");         //区分大小写
        System.out.println(b);                   //false
        System.out.println("abc".equalsIgnoreCase("ABC"));     //不区分大小写    true

        String s = "好好学习,天天向上";
        boolean b1 = s.contains("好好学习");
        System.out.println(b1);                  //true

        String s3 = "王老虎";
        System.out.println(s3.startsWith("王"));  //true
        System.out.println(s3.endsWith("老虎"));  //true

        System.out.println("".isEmpty());         //true
        System.out.println("".length() == 0);     //true
        System.out.println(s3.isEmpty());         //false


    }
}

2.转换功能的方法

  • public byte[] getBytes ()                                               把字符串转换为字节数组 
  • public char[] toCharArray ()                                         把字符串转换为字符数组 
  • public static String valueOf ( char[] chs)                      把字符数组转成字符串 
  • public static String valueOf ( int i)                                把int类型的数据转成字符串 
  • public String toLowerCase ()                                       把字符串转成小写 
  • public String toUpperCase ()                                       把字符串转成大写 
  • public String concat (String str)                                   把字符串拼接

注意:String类的valueOf方法可以把任意类型的数据转成字符串。

public class Test2 {
    public static void main(String[] args) {
      
        String str = "你好";
        //把字符串转换成字节数组
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);       //-28 -67 -96   -27 -91 -67
        }

        //字节数组 ------> 字符串
        String s = new String(bytes, 0, 6);
        System.out.println(s);                  //你好

        //字符串 ------> 字符数组
        String str2 = "把字符串转换为字符数组";
        char[] chars = str2.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);       //把 字 符 串 转 换 为 字 符 数 组
        }

        //任意类型 ------> 字符串
        int num = 100;           //把数字 100 转换成字符串 "100"
        //方法一: 拼接空串
        String s2 = num + "";    //"100"
        //方法二: valueOf()
        String s1 = String.valueOf(50);
        System.out.println(s2 + s1);         //10050
        //把字符数组转换成字符串也可以用valueOf()
        char[] chas = {'a', 'b', 'c'};
        String s5 = String.valueOf(chas);
        System.out.println(s5);         // abc 

        //把字符串转成大写
        String s6 = "abcdefgh".toUpperCase();
        System.out.println(s6);         //ABCDEFGH

        //把字符串转成小写
        String s7 = s6.toLowerCase();
        System.out.println(s7);         //abcdefgh

        //拼接字符串
        String concat = "abc".concat("ABC").concat("Hello").concat("cccc");
        System.out.println(concat);     //abcABCHellocccc
    }
}

3.获取功能的方法

  • public int length():                                                     获取字符串的长度。
  • public char charAt(int index):                                    获取指定索引位置的字符
  • public int indexOf(int ch):                                          返回指定字符在此字符串中第一次出现处的索引。
  • public int indexOf(String str):                                    返回指定字符串在此字符串中第一次出现处的索引。
  • public int indexOf(int ch,int fromIndex):                    返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  • public int indexOf(String str,int fromIndex):              返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  • public String substring(int start):                              从指定位置开始截取字符串,默认到末尾。
  • public String substring(int start,int end):                  从指定位置开始到指定位置结束截取字符串。
public class Test {
    public static void main(String[] args) {

        String s="爱生活爱java爱生活";
        //获取字符串长度
        int a=s.length();
        System.out.println(a);               //11

        //获取指定索引位置的字符(索引从0开始)
        char c=s.charAt(0);
        System.out.println(c);               //爱

        //返回指定字符在此字符串中第一次出现处的索引
        int a2=s.indexOf('爱');
        System.out.println(a2);               //0

        //返回指定字符串在此字符串中第一次出现处的索引
        int a3=s.indexOf("爱java");
        System.out.println(a3);               //3

        //返回指定字符在此字符串中从指定位置后第一次出现处的索引
        int a4=s.indexOf('爱',2);
        System.out.println(a4);               //3

        //返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
        int a5=s.indexOf("爱生活",3);
        System.out.println(a5);               //8

        //从指定位置开始截取字符串,默认到末尾
        String s2=s.substring(3);
        System.out.println(s2);               //爱java爱生活

        //从指定位置开始到指定位置结束截取字符串
        String s3=s.substring(3,8);
        System.out.println(s3);                //爱java

    }
}

4.替换功能的方法

  • public String replace ( char old, char new)                   将指定字符进行互换
  • public String replace (String old, String new)               将指定字符串进行互换

5.去除字符串两空格的方法

  • public String trim ()                                                      去除两端空格

6.按字典顺序比较两个字符串

  • public int compareTo (String str)                                       会对照ASCII 码表从第一个字母进行减法运算,返回的就是这个减法的结果.如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果.如果连个字符串一摸一样 返回的就是0.

public class Test3 {
    public static void main(String[] args) {

       //替换
        String s = "如果连个字符串一摸一样 返回的就是".replace('一', '1');
        System.out.println(s);       //如果连个字符串1摸1样 返回的就是
        String replace = "如果前面几个字母一样".replace("如果前面几个字母", "***");
        System.out.println(replace); //***一样

        //去除首位空格
        String username = "    abc     ";
        String uname = username.trim();
        System.out.println(uname);  //abc

        //比较字符串(按照字典顺序)
        String s2="我是编程小白!";
        String s3="你是编程小白!";
        String s4="我是编程小白!";
        System.out.println(s2.compareTo(s3));   //4758
        System.out.println(s2.compareTo(s4));   //0


    }
}

(小编也在努力学习更多哟!以后会多多分享哒!)

希望对友友们有所帮助!!!!