String常用方法
部分常用未作说明
public class DemoString {
public static void main(String[] args) {
String s1 = "abc";
// 1.String concat(String str)
String s2 = s1.concat("def");
System.out.println(s2); //abcdef
//2.boolean contains(CharSequence s)
boolean b1 = s2.contains(s1);
System.out.println(b1);
//3.boolean contentEquals(CharSequence cs)
StringBuilder buf = new StringBuilder("def");
boolean b2 = s2.contentEquals(buf);
System.out.println(b2); //false 可以与java.lang.CharSequence子类比较
//4.boolean equalsIgnoreCase(String anotherString)
//不区分大小写
//5.int indexOf(int ch, int fromIndex)
//返回指定字符第一次出现的字符串中的索引,从指定的索引开始搜索
//不加第二参数就是返回第一次匹配到的索引
String s3 = "bcabcabca";
int i1 = s3.indexOf("ca",3);
System.out.println(i1);
//6.boolean isEmpty()
//判空
//7.int lastIndexOf(int ch, int fromIndex)
//从后向前搜索
String s4 = "abcdefg";
int i2 = s4.lastIndexOf("ef",4); //最小为结果索引 会自动匹配
System.out.println(i2);
//8.int length()
//返回长度
//9.String replace(char oldChar, char newChar) //替换全部
//String replace(CharSequence target, CharSequence replacement) //指定索引开始
//String replaceAll(String regex, String replacement)
//10.String substring(int beginIndex, int endIndex)
String s5 = "abcdefg";
String s6 = s5.substring(2,4); //到endIndex后面一个 长度为endIndex - beginIndex
System.out.println(s6);
//11.char[] toCharArray()
//12.static String valueOf(boolean b)
}
}