字符串比较

No

方法名称

类型

描述

1

public boolean equals(Object anObject)

普通

判断字符内容是否相等,区分大小写

2

public boolean equalsIgnoreCase(String anotherString)

普通

判断字符内容是否相等,不区分大小写

3

public int compareTo(String anotherString)

普通

判断两个字符串的大小(按照字符编码比较),返回值:①=0 字符串内容相等;②>0 大于;③<0 小于

范例:判断两个字符串

public class StringCompare {
    public static void main(String[] args) {
        String str_1 = "Hello" ;
        String str_2 = "hELLO" ;
        System.out.println(str_1.equals(str_2));    //false
        System.out.println(str_1.equalsIgnoreCase(str_2));  //true
        //compareTo()方法
        if (str_1.compareTo(str_2) < 0){
            System.out.println("小于");
        }
        System.out.println(str_1.compareTo(str_2));
    }
}

字符串查找

No

方法名称

类型

描述

1

public boolean contains(CharSequence s)

普通

判断指定的内容是否存在

2

public int indexOf(int ch)

普通

由前向后查找指定字符串的位置,如果找到了则返回(第一个字母)位置索引,找不到则返回-1

3

public int indexOf(int ch, int fromIndex)

普通

由指定的位置从前向后查找指定字符串位置,找不到返回-1

4

public int lastIndexOf(int ch)

普通

从后向前查找指定字符串的位置,找不到返回-1

5

public int lastIndexOf(int ch,int fromIndex)

普通

从指定位置由后向前查找字符串的位置,找不到返回-1

6

public boolean starsWith(String suffix)

普通

判断是否以指定的字符串开头

7

public boolean startsWith(String prefix,int toffset)

普通

从指定位置开始判断是否事以指定的字符串开头

8

public boolean endsWith(String suffix)

普通

判断是否以指定的字符串结尾

范例:示例代码

public class StringSearch {
    public static void main(String[] args) {
        String str_1 = "Helloworld" ;
        //判断指定的内容是否存在
        System.out.println(str_1.contains("H"));    //true
        //查找指定字符,找到返回位置索引
        System.out.println(str_1.indexOf("o"));     //4
        //找不到返回-1
        System.out.println(str_1.indexOf("k"));     //-1
        //从指定位置开始查找,找到返回位置索引
        System.out.println(str_1.indexOf("l",4));       //8
        //从后往前查找
        System.out.println(str_1.lastIndexOf("r"));     //7
        //从指定位置,由后往前找
        System.out.println(str_1.lastIndexOf("l",5));
        //判断是否以指定的字符串开头
        System.out.println(str_1.startsWith("H"));      //true
        //从指定位置开始判断是否事以指定的字符串开头
        System.out.println(str_1.startsWith("l",4));    //false
        //判断是否以指定的字符串结尾
        System.out.println(str_1.endsWith("d"));    //true
    }
}

替换

No

方法名称

类型

描述

1

public String replace(char oldChar,char newChar)

普通

2

public String replaceAll(String regex,String replacement)

普通

用新的内容替换全部旧的内容

3

public String replaceFirst(String regex,String replacement)

普通

替换首个满足条件的内容

指使用一个新的字符串替换旧的字符串,支持的方法如下:

No

方法名称

类型

描述

1

public String replace(char oldChar,char newChar)

普通

2

public String replaceAll(String regex,String replacement)

普通

用新的内容替换全部旧的内容

3

public String replaceFirst(String regex,String replacement)

普通

替换首个满足条件的内容

public class replace {
    public static void main(String[] args) {
        String str_1 = "Hello World";

        System.out.println(str_1.replace("l" , "L"));
        //用新的内容替换全部旧的内容
        System.out.println(str_1.replaceAll("l", "L"));
        //替换首个满足条件的内容
        System.out.println(str_1.replaceFirst("l","L"));
    }
}

replace与replaceAll的区别

截取

No

方法名称

类型

描述

1

public String substring(int beginIndex)

普通

从指定索引截取到结尾

2

public String substring(int beginIndex,int endIndex)

普通

截取部分字符串数据

范例:代码示范

public class StringSubstring {
    public static void main(String[] args) {
        String str = "Hello World!";
        //从指定处截取到结尾
        System.out.println(str.substring(5));   // World!
        //截取部分字符串
        System.out.println(str.substring(0,5));  //Hello
    }
}

拆分

No

方法名称

类型

描述

1

public String[] split(String regex)

普通

按照指定的字符串进行全部拆分

2

public String[] split(String regex,int limit)

普通

范例:代码示范

public class StringSplit {
    public static void main(String[] args) {
        String str = "Hello World!";
        String str2 = "Hello World Nice !";
        String[] result_1 = str.split(" ");
        String[] result_2 = str2.split(" ",2);
        for (int x = 0 ; x < result_1.length ; x++){
            System.out.println(result_1[x]);
            System.out.println(result_2[x]);
        }
    }
}