首先String有不可变性,是final修饰的,一旦初始化便不可变,
String s1 = "SpringBoot";
String s2 = "SpringCloud";
String s3 = "SpringBootSpringBoot";
String s4 = "SpringBoot" + "SpringCloud";
String s5 = s1 + "SpringCloud";
String s6 = "SpringBoot" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);
System.out.println(s3 == s5);
System.out.println(s3 == s6);
System.out.println(s3 == s7);
System.out.println(s5 == s6);
System.out.println(s5 == s7);
System.out.println(s6 == s7);
常用方法一
s1.length()
s1.charAt(index)
s1.isEmpty()
String s3 = " learn java EE ";
String s4 = s3.trim();
s3.trim();
s3.concat(" after SpringCloud");
String s6 = "SpringBoot";
String s7 = "SpringCloud";
s6.compareTo(s7)
s6.substring(0, 6);
String s1 = "SpringBoot";
boolean b = s1.endsWith("Boot");
boolean b1 = s1.startsWith("Spring");
boolean b2 = s1.startsWith("Boot", 6);
String s2 = "Spring";
System.out.println(s1.contains(s2));
System.out.println(s1.indexOf("Bo"));
System.out.println(s1.indexOf("Bo",3));
System.out.println(s1.lastIndexOf("B"));
常用方法二
String s1 = "SpringBoot";
String s2 = "SpringCloud";
String s = s1.replace("Boot", "Cloud");
String str1 = "123";
int i = Integer.parseInt(str1);
System.out.println(i);
String str2 = String.valueOf(i);
System.out.println(str2);
String str3 = "abc123";
char[] chars = str3.toCharArray();
for (int j = 0; j < chars.length; j++) {
System.out.println(chars[j]);
}
char[] arr =new char[]{'h','e','l','l','o'};
String str4 = new String(arr);
System.out.println(str4);
String str1 = "abc123";
byte[] bytes = str1.getBytes();
System.out.println(Arrays.toString(bytes));