String类常用方法(字符串比较、查找、替换、截取、拆分)

字符串比较

No方法名称类型描述
1public boolean equals(Object anObject)普通判断字符内容是否相等,区分大小写
2public boolean equalsIgnoreCase(String anotherString)普通判断字符内容是否相等,不区分大小写
3public 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方法名称类型描述
1public boolean contains(CharSequence s)普通判断指定的内容是否存在
2public int indexOf(int ch)普通由前向后查找指定字符串的位置,如果找到了则返回(第一个字母)位置索引,找不到则返回-1
3public int indexOf(int ch, int fromIndex)普通由指定的位置从前向后查找指定字符串位置,找不到返回-1
4public int lastIndexOf(int ch)普通从后向前查找指定字符串的位置,找不到返回-1
5public int lastIndexOf(int ch,int fromIndex)普通从指定位置由后向前查找字符串的位置,找不到返回-1
6public boolean starsWith(String suffix)普通判断是否以指定的字符串开头
7public boolean startsWith(String prefix,int toffset)普通从指定位置开始判断是否事以指定的字符串开头
8public 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方法名称类型描述
1public String replace(char oldChar,char newChar)普通
2public String replaceAll(String regex,String replacement)普通用新的内容替换全部旧的内容
3public String replaceFirst(String regex,String replacement)普通替换首个满足条件的内容

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

No方法名称类型描述
1public String replace(char oldChar,char newChar)普通
2public String replaceAll(String regex,String replacement)普通用新的内容替换全部旧的内容
3public 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方法名称类型描述
1public String substring(int beginIndex)普通从指定索引截取到结尾
2public 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方法名称类型描述
1public String[] split(String regex)普通按照指定的字符串进行全部拆分
2public 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]);
        }
    }
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值