String类的常用方法

2025-07-02 20:35:28

1、获取字符串的长度方法:public int length()实例:String str = new String("abcdefg");int length = str.length();

String类的常用方法

3、查找子串在字腿发滏呸符串中的位置方法:public int indexOf(String str)用于查找当前字符串中字符或子串,子串在当前字符串中从左边起首次出现的位置,若没有出现嬴猹缥犴则返回-1。public int indexOf(String str, intfromIndex)方法与第一种类似,区别在于该方法从fromIndex位置向后查找。public int lastIndexOf(String str)实例:Stringstr = new String("abcdefbg"); int index = str.indexOf("b");int lastIndex = str.lastIndexOf("b");

String类的常用方法

5、字符串比较方法:该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。public int compareTo(String anotherString)与compareTo方法相似,但忽略大小写。public int compareToIgnore(String anotherString)比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。public boolean equals(Object anotherObject)

String类的常用方法
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢