Java String 类
字符串的创建、长度、连接、常用方法与格式化。
创建字符串
String literals live in the constant pool and identical literals share one object, while new String(...) allocates on the heap. That is why == is sometimes true and sometimes false.
public class StringCreate {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
System.out.println(s1.length());
}
}true
false
true
5字符串长度
length() returns the number of characters (UTF-16 code units, not bytes). Arrays use the length field while strings use the length() method — do not mix them up.
public class StringLength {
public static void main(String[] args) {
String s = "BackendWiki";
System.out.println(s.length());
System.out.println("".length());
System.out.println(s.isEmpty());
System.out.println(" ".isBlank());
}
}11
0
false
true连接字符串
+ is the most direct and compiles to StringBuilder; concat() only accepts strings and does not handle null; for heavy concatenation use StringBuilder explicitly.
public class StringConcat {
public static void main(String[] args) {
String a = "Hello";
String b = "World";
System.out.println(a + ", " + b + "!");
System.out.println(a.concat(" ").concat(b));
System.out.println(a + 1 + 2);
System.out.println(1 + 2 + a);
}
}Hello, World!
Hello World
Hello12
3Hello常用方法
The everyday set: substring, indexOf, contains, startsWith, replace, split, trim, toUpperCase, equalsIgnoreCase. All return new objects; the original never changes.
import java.util.Arrays;
public class StringMethods {
public static void main(String[] args) {
String s = " Hello,Java ";
System.out.println(s.trim());
System.out.println(s.trim().substring(0, 5));
System.out.println(s.contains("Java"));
System.out.println(s.indexOf("Java"));
System.out.println(s.trim().replace("Java", "World"));
System.out.println(Arrays.toString(s.trim().split(",")));
System.out.println("abc".equalsIgnoreCase("ABC"));
}
}Hello,Java
Hello
true
8
Hello,World
[Hello, Java]
true格式化字符串
String.format shares specifiers with printf: %s string, %d integer, %.2f two decimals, %5d width 5, %-5s left-aligned, %n newline.
public class StringFormat {
public static void main(String[] args) {
String name = "Tom";
int age = 28;
double score = 91.456;
System.out.println(String.format("name=%s age=%d", name, age));
System.out.printf("score=%.1f%n", score);
System.out.println(String.format("[%5d]", 42));
System.out.println(String.format("[%-5s]", "ab"));
System.out.println("x".repeat(3));
}
}name=Tom age=28
score=91.5
[ 42]
[ab ]
xxx创建字符串
字符串字面量放在常量池,相同字面量复用同一对象;new String(...) 则在堆上新开一块。这就是 == 有时为 true 有时为 false 的原因。
public class StringCreate {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
System.out.println(s1.length());
}
}true
false
true
5字符串长度
length() 返回字符个数(UTF-16 码元数,不是字节数)。注意数组用 arr.length 属性,字符串用 length() 方法,两者写法不同。
public class StringLength {
public static void main(String[] args) {
String s = "BackendWiki";
System.out.println(s.length());
System.out.println("".length());
System.out.println(s.isEmpty());
System.out.println(" ".isBlank());
}
}11
0
false
true连接字符串
+ 拼接最直观,底层编译成 StringBuilder;concat() 只接受字符串且不处理 null;大量拼接应显式用 StringBuilder。
public class StringConcat {
public static void main(String[] args) {
String a = "Hello";
String b = "World";
System.out.println(a + ", " + b + "!");
System.out.println(a.concat(" ").concat(b));
System.out.println(a + 1 + 2);
System.out.println(1 + 2 + a);
}
}Hello, World!
Hello World
Hello12
3Hello常用方法
高频方法:substring、indexOf、contains、startsWith、replace、split、trim、toUpperCase、equalsIgnoreCase。字符串方法都返回新对象,不修改原串。
import java.util.Arrays;
public class StringMethods {
public static void main(String[] args) {
String s = " Hello,Java ";
System.out.println(s.trim());
System.out.println(s.trim().substring(0, 5));
System.out.println(s.contains("Java"));
System.out.println(s.indexOf("Java"));
System.out.println(s.trim().replace("Java", "World"));
System.out.println(Arrays.toString(s.trim().split(",")));
System.out.println("abc".equalsIgnoreCase("ABC"));
}
}Hello,Java
Hello
true
8
Hello,World
[Hello, Java]
true格式化字符串
String.format 与 printf 共用格式说明符:%s 字符串、%d 整数、%.2f 两位小数、%5d 宽度 5、%-5s 左对齐、%n 换行。
public class StringFormat {
public static void main(String[] args) {
String name = "Tom";
int age = 28;
double score = 91.456;
System.out.println(String.format("name=%s age=%d", name, age));
System.out.printf("score=%.1f%n", score);
System.out.println(String.format("[%5d]", 42));
System.out.println(String.format("[%-5s]", "ab"));
System.out.println("x".repeat(3));
}
}name=Tom age=28
score=91.5
[ 42]
[ab ]
xxx