JDK1.5版本以上
eclipse等开发工具
获取字符串的长度 方法:public int length()实例:String str = new String('abcdefg');int length = str.length();
获取字符串某一位置字符方法:返回字符串中指定位置的字符;public char charAt(int index)实例:String str = new String('abcdefg'); char ch = str.charAt(3);序号是从0 开始的,所以chatAt(3)是字符d
查找子串在字符串中的位置 方法: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');
截取字符串的子串 方法:从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回public String substring(int beginIndex)该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。public String substring(int beginIndex, int endIndex)相似equals方法,但忽略大小写。public boolean equalsIgnoreCase(String anotherString)实例:String str = new String('abcdefbg'); String str2 = str.substring(3); String str3 = str.substring(3,5);
字符串比较方法:该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。public int compareTo(String anotherString)与compareTo方法相似,但忽略大小写。public int compareToIgnore(String anotherString)比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。public boolean equals(Object anotherObject)
对字符串中的字符进行大小写转换方法:转换成大写public String toUpperCase()转换成小写public String toLowerCase()实例: String str1 = new String('bcdFg'); String str2 = str1.toLowerCase(); String str3 = str1.toUpperCase();