Java String 类

字符串的创建、长度、连接、常用方法与格式化。

Beginner
Key PointsString 不可变,每次「修改」都返回新对象;比较内容用 equals;拼接用 + 在循环里会大量产生垃圾。

创建字符串

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.

Java
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());
    }
}
Output
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.

Java
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());
    }
}
Output
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.

Java
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);
    }
}
Output
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.

Java
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"));
    }
}
Output
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.

Java
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));
    }
}
Output
name=Tom age=28
score=91.5
[   42]
[ab   ]
xxx

Friends & Partners