Forpersonaluseonlyinstudyandresearch;mercialusejava中String的常用方法java中String的常用方法 1、length() 字符串的长度 例:charchars[]={'a','b'.'c'}; Strings=newString(chars); intlen=(); 2、charAt() 截取一个字符 例:charch; ch="abc".charAt(1);返回'b' 3、getChars() 截取多个字符 voidgetChars(intsourceStart,intsourceEnd,chartarget[],inttargetStart) sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。 例:Strings="thisisademoofthegetCharsmethod."; charbuf[]=newchar[20]; (10,14,buf,0); 4、getBytes() 替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()。 5、toCharArray() 6、equals()和equalsIgnoreCase() 比较两个字符串 7、regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。 booleanregionMatches(intstartIndex,Stringstr2,intstr2StartIndex,intnumChars) booleanregionMatches(booleanignoreCase,intstartIndex,Stringstr2,intstr2StartIndex,intnumChars) 8、startsWith()和endsWith() startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束 9、equals()和== equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。 例:Strings1="Hello"; Strings2=newString(s1); (s2);//true s1==s2;//false pareTo()和compareToIgnoreCase() 比较字符串 11、indexOf()和lastIndexOf() indexOf()查找字符或者子串第一次出现的地方。 lastIndexOf()查找字符或者子串是后一次出现的地方。 12、substring() 它有两种形式,第一种是:Stringsubstring(intstartIndex) 第二种是:Stringsubstring(intstartIndex,intendIndex) 13、concat() 连接两个字符串 14、replace() 替换 它有两种形式,第一种
java中String的常用方法 来自淘豆网m.daumloan.com转载请标明出处.