网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

String类的常用方法

时间:2024-10-27 17:53:16

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类的常用方法
© 2025 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com