Java : String 示例

String (Java SE 22 & JDK 22) 示例。
您将在大多数 String 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

String 类表示字符串。Java 程序中的所有字符串文字(例如“abc”)都是作为此类的实例实现的。 (机器翻译)

Class diagram

final var s = "abcd";
System.out.println(s); // "abcd"

final var upper = s.toUpperCase();
System.out.println(upper); // "ABCD"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"
System.out.println(surrogate.length()); // 2

final var chars = surrogate.chars().toArray();
System.out.println(Arrays.toString(chars)); // [55357, 56842]

final var codePoints = surrogate.codePoints().toArray();
System.out.println(Arrays.toString(codePoints)); // [128522]

Fields

static final Comparator<String> CASE_INSENSITIVE_ORDER

通过 compareToIgnoreCase 对 String 对象进行排序的比较器。 (机器翻译)

final var list = new ArrayList<String>();
list.add("Tree");
list.add("book");
list.add("stone");
list.add("Cake");

System.out.println(list); // [Tree, book, stone, Cake]

list.sort(Comparator.naturalOrder());
System.out.println(list); // [Cake, Tree, book, stone]

list.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println(list); // [book, Cake, stone, Tree]
final var list = new ArrayList<String>();
list.add("D");
list.add("c");
list.add("B");
list.add("a");

System.out.println(list); // [D, c, B, a]

list.sort(Comparator.naturalOrder());
System.out.println(list); // [B, D, a, c]

list.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println(list); // [a, B, c, D]

Constructors

String ()

初始化新创建的 String 对象,使其表示一个空字符序列。 (机器翻译)

// Recommended.
final var s = "";
System.out.println(s); // ""
System.out.println(s.isEmpty()); // true
// Unrecommended.
final var s = new String();
System.out.println(s); // ""
System.out.println(s.isEmpty()); // true

String (byte[] bytes)

通过使用默认字符集解码指定的字节数组来构造一个新的字符串。 (机器翻译)

final var bytes = "abcde".getBytes();
System.out.println(Arrays.toString(bytes)); // [97, 98, 99, 100, 101]

final var s = new String(bytes);
System.out.println(s); // "abcde"
System.out.println(Charset.defaultCharset()); // UTF-8

final var sjis = "☆◇△".getBytes("Shift_JIS");

// [-127, -103, -127, -98, -127, -94]
System.out.println(Arrays.toString(sjis));
System.out.println(new String(sjis)); // "������" <- Garbled characters.

final var utf8 = "☆◇△".getBytes(StandardCharsets.UTF_8);

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(utf8));
System.out.println(new String(utf8)); // "☆◇△"

String (byte[] ascii, int hibyte)

已弃用。此方法无法正确将字节转换为字符。 (机器翻译)

Deprecated.

String (byte[] bytes, int offset, int length)

通过使用默认字符集解码指定的字节子数组来构造一个新的字符串。 (机器翻译)

final var bytes = "abcd".getBytes();
System.out.println(Arrays.toString(bytes)); // [97, 98, 99, 100]

System.out.println(new String(bytes, 0, 0)); // ""
System.out.println(new String(bytes, 0, 1)); // "a"
System.out.println(new String(bytes, 0, 2)); // "ab"
System.out.println(new String(bytes, 0, 3)); // "abc"
System.out.println(new String(bytes, 0, 4)); // "abcd"

System.out.println(new String(bytes, 1, 3)); // "bcd"
System.out.println(new String(bytes, 2, 2)); // "cd"
System.out.println(new String(bytes, 3, 1)); // "d"
System.out.println(new String(bytes, 4, 0)); // ""
System.out.println(Charset.defaultCharset()); // UTF-8

final var bytes = "☆◇△".getBytes();

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(bytes));

final var s = new String(bytes, 3, 6);
System.out.println(s); // "◇△"

String (byte[] ascii, int hibyte, int offset, int count)

已弃用。此方法无法正确将字节转换为字符。 (机器翻译)

Deprecated.

String (byte[] bytes, int offset, int length, String charsetName)

通过使用指定的字符集解码指定的字节子数组来构造一个新的字符串。 (机器翻译)

Please see the methods below.

String (byte[] bytes, int offset, int length, Charset charset)

通过使用指定的字符集解码指定的字节子数组来构造一个新的字符串。 (机器翻译)

Please see the methods below.

String (byte[] bytes, String charsetName)

通过使用指定的字符集解码指定的字节数组来构造一个新的字符串。 (机器翻译)

final var bytes = "abcd".getBytes("UTF-8");
System.out.println(Arrays.toString(bytes)); // [97, 98, 99, 100]

System.out.println(new String(bytes, "UTF-8")); // "abcd"
final var sjis = "☆◇△".getBytes("Shift_JIS");

// [-127, -103, -127, -98, -127, -94]
System.out.println(Arrays.toString(sjis));
System.out.println(new String(sjis, "Shift_JIS")); // "☆◇△"

final var utf8 = "☆◇△".getBytes("UTF-8");

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(utf8));
System.out.println(new String(utf8, "UTF-8")); // "☆◇△"

String (byte[] bytes, Charset charset)

通过使用指定的字符集解码指定的字节数组来构造一个新的字符串。 (机器翻译)

final var bytes = "abcd".getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(bytes)); // [97, 98, 99, 100]

System.out.println(new String(bytes, StandardCharsets.UTF_8)); // "abcd"
final var sjis = "☆◇△".getBytes(Charset.forName("Shift_JIS"));

// [-127, -103, -127, -98, -127, -94]
System.out.println(Arrays.toString(sjis));
System.out.println(new String(sjis, Charset.forName("Shift_JIS"))); // "☆◇△"

final var utf8 = "☆◇△".getBytes(StandardCharsets.UTF_8);

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(utf8));
System.out.println(new String(utf8, StandardCharsets.UTF_8)); // "☆◇△"

String (char[] value)

分配一个新的字符串,使其表示字符数组参数当前包含的字符序列。 (机器翻译)

final char[] value = {'a', 'b', 'c', 'd'};
System.out.println(Arrays.toString(value)); // [a, b, c, d]

final var s = new String(value);
System.out.println(s); // "abcd"
final char[] value = {'☆', '◇', '△'};
System.out.println(Arrays.toString(value)); // [☆, ◇, △]

final var s = new String(value);
System.out.println(s); // "☆◇△"

String (char[] value, int offset, int count)

分配一个包含字符数组参数子数组中的字符的新字符串。 (机器翻译)

final char[] value = {'a', 'b', 'c', 'd'};
System.out.println(Arrays.toString(value)); // [a, b, c, d]

System.out.println(new String(value, 0, 0)); // ""
System.out.println(new String(value, 0, 1)); // "a"
System.out.println(new String(value, 0, 2)); // "ab"
System.out.println(new String(value, 0, 3)); // "abc"
System.out.println(new String(value, 0, 4)); // "abcd"

System.out.println(new String(value, 1, 3)); // "bcd"
System.out.println(new String(value, 2, 2)); // "cd"
System.out.println(new String(value, 3, 1)); // "d"
System.out.println(new String(value, 4, 0)); // ""
final char[] value = {'☆', '◇', '△', '▽'};
System.out.println(Arrays.toString(value)); // [☆, ◇, △, ▽]

final var s = new String(value, 1, 2);
System.out.println(s); // "◇△"

String (int[] codePoints, int offset, int count)

分配一个新字符串,其中包含来自 Unicode 代码点数组参数的子数组的字符。 (机器翻译)

final var codePoints = "abcd".codePoints().toArray();
System.out.println(Arrays.toString(codePoints)); // [97, 98, 99, 100]

System.out.println(new String(codePoints, 0, 0)); // ""
System.out.println(new String(codePoints, 0, 1)); // "a"
System.out.println(new String(codePoints, 0, 2)); // "ab"
System.out.println(new String(codePoints, 0, 3)); // "abc"
System.out.println(new String(codePoints, 0, 4)); // "abcd"

System.out.println(new String(codePoints, 1, 3)); // "bcd"
System.out.println(new String(codePoints, 2, 2)); // "cd"
System.out.println(new String(codePoints, 3, 1)); // "d"
System.out.println(new String(codePoints, 4, 0)); // ""
// An example with a surrogate code point.
final var str = "ab\uD83D\uDE0Acd";
System.out.println(str); // "ab😊cd"

final var codePoints = str.codePoints().toArray();
System.out.println(Arrays.toString(codePoints)); // [97, 98, 128522, 99, 100]

final var s = new String(codePoints, 1, 3);
System.out.println(s); // "b😊c"

String (String original)

初始化新创建的 String 对象,使其表示与参数相同的字符序列;换句话说,新创建的字符串是参数字符串的副本。 (机器翻译)

// Recommended.
final var s = "abcd";
System.out.println(s); // "abcd"
// Unrecommended.
final var s = new String("abcd");
System.out.println(s); // "abcd"

String (StringBuffer buffer)

分配一个新字符串,其中包含字符串缓冲区参数当前包含的字符序列。 (机器翻译)

final var buffer = new StringBuffer();
buffer.append("num : ");
buffer.append(123);

final var s = new String(buffer);
System.out.println(s); // "num : 123"

String (StringBuilder builder)

分配一个新字符串,其中包含字符串构建器参数当前包含的字符序列。 (机器翻译)

final var builder = new StringBuilder();
builder.append("num : ");
builder.append(123);

final var s = new String(builder);
System.out.println(s); // "num : 123"

Methods

char charAt (int index)

返回指定索引处的字符值。 (机器翻译)

final var s = "abc";
//s.charAt(-1); // IndexOutOfBoundsException
System.out.println(s.charAt(0)); // a
System.out.println(s.charAt(1)); // b
System.out.println(s.charAt(2)); // c
//s.charAt(3); // IndexOutOfBoundsException
final var s = "☆◇△";
System.out.println(s.charAt(0)); // ☆
System.out.println(s.charAt(1)); // ◇
System.out.println(s.charAt(2)); // △
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = surrogate + "ab";
System.out.println(s); // "😊ab"

final var ret1 = s.charAt(0);
System.out.println(ret1); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret1)); // d83d

final var ret2 = s.charAt(1);
System.out.println(ret2); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret2)); // de0a

final var ret3 = s.charAt(2);
System.out.println(ret3); // a

final var ret4 = s.charAt(3);
System.out.println(ret4); // b

//s.charAt(4); // IndexOutOfBoundsException

IntStream chars ()

返回对此序列的字符值进行零扩展的 int 流。 (机器翻译)

// Unicode : 0x61 = a, 0x62 = b, 0x63 = c, 0x64 = d, 0x65 = e
final var ret = "abcde".chars();

// [61, 62, 63, 64, 65]
System.out.println(ret.mapToObj(Integer::toHexString).toList());
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "ab" + surrogate;
System.out.println(s); // "ab😊"

final var ret = s.chars();

ret.forEach(c -> {
    System.out.printf("%x : %c%n", c, c);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "d83d : ?" <- Garbled characters.
// "de0a : ?" <- Garbled characters.

int codePointAt (int index)

返回指定索引处的字符(Unicode 代码点)。 (机器翻译)

final var s = "abc";
//s.codePointAt(-1); // IndexOutOfBoundsException
System.out.printf("%c%n", s.codePointAt(0)); // a
System.out.printf("%c%n", s.codePointAt(1)); // b
System.out.printf("%c%n", s.codePointAt(2)); // c
//s.codePointAt(3); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = surrogate + "ab";
System.out.println(s); // "😊ab"

final var ret1 = s.codePointAt(0);
System.out.printf("%c%n", ret1); // 😊
System.out.println(Integer.toHexString(ret1)); // 1f60a

final var ret2 = s.codePointAt(1);
System.out.printf("%c%n", ret2); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret2)); // de0a

final var ret3 = s.codePointAt(2);
System.out.printf("%c%n", ret3); // a

final var ret4 = s.codePointAt(3);
System.out.printf("%c%n", ret4); // b

//s.codePointAt(4); // IndexOutOfBoundsException

int codePointBefore (int index)

返回指定索引之前的字符(Unicode 代码点)。 (机器翻译)

final var s = "abc";

//s.codePointBefore(0); // IndexOutOfBoundsException
System.out.printf("%c%n", s.codePointBefore(1));  // a
System.out.printf("%c%n", s.codePointBefore(2));  // b
System.out.printf("%c%n", s.codePointBefore(3));  // c
//s.codePointBefore(4); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = surrogate + "ab";
System.out.println(s); // "😊ab"

//s.codePointBefore(0); // IndexOutOfBoundsException

final var ret1 = s.codePointBefore(1);
System.out.printf("%c%n", ret1); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret1)); // d83d

final var ret2 = s.codePointBefore(2);
System.out.printf("%c%n", ret2); // 😊
System.out.println(Integer.toHexString(ret2)); // 1f60a

final var ret3 = s.codePointBefore(3);
System.out.printf("%c%n", ret3); // a

final var ret4 = s.codePointBefore(4);
System.out.printf("%c%n", ret4); // b

//s.codePointBefore(5); // IndexOutOfBoundsException

int codePointCount (int beginIndex, int endIndex)

返回此字符串的指定文本范围内的 Unicode 代码点数。 (机器翻译)

final var s = "abc";
System.out.println(s.codePointCount(0, 3));  // 3
System.out.println(s.codePointCount(1, 2));  // 1
//s.codePointCount(-1, 3);  // IndexOutOfBoundsException
//s.codePointCount(0, 4);  // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = surrogate + "ab";
System.out.println(s); // "😊ab"

System.out.println(s.codePointCount(0, 1));  // 1
System.out.println(s.codePointCount(0, 2));  // 1
System.out.println(s.codePointCount(0, 3));  // 2
System.out.println(s.codePointCount(0, 4));  // 3
//s.codePointCount(0, 5);  // IndexOutOfBoundsException

IntStream codePoints ()

返回此序列中的代码点值流。 (机器翻译)

// Unicode : 0x61 = a, 0x62 = b, 0x63 = c, 0x64 = d, 0x65 = e
final var ret = "abcde".codePoints();

// [61, 62, 63, 64, 65]
System.out.println(ret.mapToObj(Integer::toHexString).toList());
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "ab" + surrogate;
System.out.println(s); // "ab😊"

final var ret = s.codePoints();

ret.forEach(c -> {
    System.out.printf("%x : %c%n", c, c);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "1f60a : 😊"

int compareTo (String anotherString)

按字典顺序比较两个字符串。 (机器翻译)

System.out.println("Cake".compareTo("Book")); // 1
System.out.println("Cake".compareTo("Stone")); // -16

System.out.println("Book".compareTo("Book")); // 0
System.out.println("Book".compareTo("book")); // -32
System.out.println("a".compareTo("a")); // 0
System.out.println("a".compareTo("A")); // 32
System.out.println("A".compareTo("a")); // -32
System.out.println("A".compareTo("A")); // 0

System.out.println("a".compareTo("b")); // -1
System.out.println("b".compareTo("a")); // 1

System.out.println("A".compareTo("b")); // -33
System.out.println("b".compareTo("A")); // 33

System.out.println("a".compareTo("B")); // 31
System.out.println("B".compareTo("a")); // -31

int compareToIgnoreCase (String str)

按字典顺序比较两个字符串,忽略大小写差异。 (机器翻译)

System.out.println("Cake".compareToIgnoreCase("Book")); // 1
System.out.println("Cake".compareToIgnoreCase("Stone")); // -16

System.out.println("Book".compareToIgnoreCase("Book")); // 0
System.out.println("Book".compareToIgnoreCase("book")); // 0
System.out.println("a".compareToIgnoreCase("a")); // 0
System.out.println("a".compareToIgnoreCase("A")); // 0
System.out.println("A".compareToIgnoreCase("a")); // 0
System.out.println("A".compareToIgnoreCase("A")); // 0

System.out.println("a".compareToIgnoreCase("b")); // -1
System.out.println("b".compareToIgnoreCase("a")); // 1

System.out.println("A".compareToIgnoreCase("b")); // -1
System.out.println("b".compareToIgnoreCase("A")); // 1

System.out.println("a".compareToIgnoreCase("B")); // -1
System.out.println("B".compareToIgnoreCase("a")); // 1

String concat (String str)

将指定的字符串连接到此字符串的末尾。 (机器翻译)

final var ret1 = "cares".concat("s");
System.out.println(ret1); // "caress"

final var ret2 = "to".concat("get").concat("her");
System.out.println(ret2); // "together"

boolean contains (CharSequence s)

当且仅当此字符串包含指定的 char 值序列时才返回 true。 (机器翻译)

final var s = "abcde";

System.out.println(s.contains("a")); // true
System.out.println(s.contains("abc")); // true
System.out.println(s.contains("z")); // false
System.out.println(s.contains("ace")); // false
final var s = "☆◇△";

System.out.println(s.contains("☆")); // true
System.out.println(s.contains("◇△")); // true
System.out.println(s.contains("▽")); // false
System.out.println(s.contains("a")); // false

boolean contentEquals (CharSequence cs)

将此字符串与指定的 CharSequence 进行比较。 (机器翻译)

final var s = "abc";
System.out.println(s.contentEquals("abc")); // true
System.out.println(s.contentEquals("xyz")); // false

final var cs = new StringBuilder();
cs.append('a');

System.out.println(s.contentEquals(cs)); // false

cs.append('b');
cs.append('c');

System.out.println(s.contentEquals(cs)); // true

boolean contentEquals (StringBuffer sb)

将此字符串与指定的 StringBuffer 进行比较。 (机器翻译)

final var s = "abc";

final var sb = new StringBuffer();
sb.append('a');

System.out.println(s.contentEquals(sb)); // false

sb.append('b');
sb.append('c');

System.out.println(s.contentEquals(sb)); // true

static String copyValueOf (char[] data)

相当于valueOf(char[])。 (机器翻译)

final char[] data = {'a', 'b', 'c'};
final var ret = String.copyValueOf(data);

System.out.println(ret); // "abc"
final char[] data = {'☆', '◇', '△'};
final var ret = String.copyValueOf(data);

System.out.println(ret); // "☆◇△"

static String copyValueOf (char[] data, int offset, int count)

相当于valueOf(char[], int, int)。 (机器翻译)

final char[] data = {'a', 'b', 'c'};

System.out.println(String.copyValueOf(data, 0, 0)); // ""
System.out.println(String.copyValueOf(data, 0, 1)); // "a"
System.out.println(String.copyValueOf(data, 0, 2)); // "ab"
System.out.println(String.copyValueOf(data, 0, 3)); // "abc"
//String.copyValueOf(data, 0, 4); // IndexOutOfBoundsException

System.out.println(String.copyValueOf(data, 1, 2)); // "bc"
System.out.println(String.copyValueOf(data, 2, 1)); // "c"
final char[] data = {'☆', '◇', '△'};

System.out.println(String.copyValueOf(data, 0, 3)); // "☆◇△"
System.out.println(String.copyValueOf(data, 1, 2)); // "◇△"

Optional<String> describeConstable ()

返回一个包含此实例的名义描述符的可选对象,该描述符就是实例本身。 (机器翻译)

final var ret = "abcd".describeConstable();
System.out.println(ret); // Optional[abcd]

boolean endsWith (String suffix)

测试此字符串是否以指定的后缀结尾。 (机器翻译)

final var s = "abcd";

System.out.println(s.endsWith("abcd")); // true
System.out.println(s.endsWith("bcd")); // true
System.out.println(s.endsWith("cd")); // true
System.out.println(s.endsWith("d")); // true
System.out.println(s.endsWith("")); // true

System.out.println(s.endsWith("a")); // false
System.out.println(s.endsWith("ab")); // false
System.out.println(s.endsWith("abc")); // false

boolean equals (Object anObject)

将此字符串与指定对象进行比较。 (机器翻译)

final var s = "abc";

System.out.println(s.equals("a")); // false
System.out.println(s.equals("ab")); // false
System.out.println(s.equals("abc")); // true
System.out.println(s.equals("abcd")); // false

System.out.println(s.equals("ABC")); // false
final var s = "abc";
final var sb = new StringBuilder("abc");

System.out.println(s.equals(sb)); // false
System.out.println(s.contentEquals(sb)); // true

boolean equalsIgnoreCase (String anotherString)

将此字符串与另一个字符串进行比较,忽略大小写。 (机器翻译)

final var s = "abc";

System.out.println(s.equalsIgnoreCase("")); // false
System.out.println(s.equalsIgnoreCase("a")); // false
System.out.println(s.equalsIgnoreCase("ab")); // false
System.out.println(s.equalsIgnoreCase("abc")); // true
System.out.println(s.equalsIgnoreCase("abcd")); // false

System.out.println(s.equalsIgnoreCase("ABC")); // true

static String format (String format, Object... args)

使用指定的格式字符串和参数返回格式化的字符串。 (机器翻译)

System.out.println(String.format("b : %b", true));  // "b : true"
System.out.println(String.format("B : %B", false)); // "B : FALSE"

System.out.println(String.format("h : %h", "abcd")); // "h : 2d9442"
System.out.println(String.format("H : %H", LocalTime.of(12, 30))); // "H : 6103F8ED"

System.out.println(String.format("s : %s", "abcd")); // "s : abcd"
System.out.println(String.format("S : %S", "abcd")); // "S : ABCD"

System.out.println(String.format("c : %c", 'z')); // "c : z"
System.out.println(String.format("C : %C", 'z')); // "C : Z"

System.out.println(String.format("d : %d", 123)); // "d : 123"
System.out.println(String.format("o : %o", 123)); // "o : 173"

System.out.println(String.format("x : %x", 123)); // "x : 7b"
System.out.println(String.format("X : %X", 123)); // "X : 7B"

System.out.println(String.format("e : %e", 0.12)); // "e : 1.200000e-01"
System.out.println(String.format("E : %E", 0.12)); // "E : 1.200000E-01"

System.out.println(String.format("f : %f", 1.400004)); // "f : 1.400004"
System.out.println(String.format("g : %g", 1.400004)); // "g : 1.40000"

System.out.println(String.format("a : %a", 0.12)); // "a : 0x1.eb851eb851eb8p-4"
System.out.println(String.format("A : %A", 0.12)); // "A : 0X1.EB851EB851EB8P-4"

final var date = LocalDate.of(2100, 3, 10);
System.out.println(date); // 2100-03-10
System.out.println(String.format("%tY %tm %te", date, date, date)); // "2100 03 10"

static String format (Locale l, String format, Object... args)

使用指定的语言环境、格式字符串和参数返回格式化的字符串。 (机器翻译)

final var date = LocalDate.of(2100, 3, 10);
System.out.println(date); // 2100-03-10

final var ret = String.format(
        Locale.ENGLISH, "%tY %tB %te %tA", date, date, date, date);
System.out.println(ret); // 2100 March 10 Wednesday

String formatted (Object... args)

使用此字符串作为格式字符串和提供的参数的格式。 (机器翻译)

System.out.println("b : %b".formatted(true));  // "b : true"
System.out.println("B : %B".formatted(false)); // "B : FALSE"

System.out.println("h : %h".formatted("abcd")); // "h : 2d9442"
System.out.println("H : %H".formatted(LocalTime.of(12, 30))); // "H : 6103F8ED"

System.out.println("s : %s".formatted("abcd")); // "s : abcd"
System.out.println("S : %S".formatted("abcd")); // "S : ABCD"

System.out.println("c : %c".formatted('z')); // "c : z"
System.out.println("C : %C".formatted('z')); // "C : Z"

System.out.println("d : %d".formatted(123)); // "d : 123"
System.out.println("o : %o".formatted(123)); // "o : 173"

System.out.println("x : %x".formatted(123)); // "x : 7b"
System.out.println("X : %X".formatted(123)); // "X : 7B"

System.out.println("e : %e".formatted(0.12)); // "e : 1.200000e-01"
System.out.println("E : %E".formatted(0.12)); // "E : 1.200000E-01"

System.out.println("f : %f".formatted(1.400004)); // "f : 1.400004"
System.out.println("g : %g".formatted(1.400004)); // "g : 1.40000"

System.out.println("a : %a".formatted(0.12)); // "a : 0x1.eb851eb851eb8p-4"
System.out.println("A : %A".formatted(0.12)); // "A : 0X1.EB851EB851EB8P-4"

final var date = LocalDate.of(2100, 3, 10);
System.out.println(date); // 2100-03-10
System.out.println("%tY %tm %te".formatted(date, date, date)); // "2100 03 10"

byte[] getBytes ()

使用默认字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。 (机器翻译)

final var ret = "abcd".getBytes();

// [97, 98, 99, 100]
System.out.println(Arrays.toString(ret));
System.out.println(Charset.defaultCharset()); // UTF-8

final var s = "☆◇△";
final var ret = s.getBytes();

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(ret));

final var utf8 = s.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.equals(ret, utf8)); // true

final var sjis = s.getBytes("Shift_JIS");
System.out.println(Arrays.equals(ret, sjis)); // false

void getBytes (int srcBegin, int srcEnd, byte[] dst, int dstBegin)

已弃用。此方法无法正确将字符转换为字节。 (机器翻译)

Deprecated.

byte[] getBytes (String charsetName)

使用命名的字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。 (机器翻译)

final var ret = "abcd".getBytes("UTF-8");

// [97, 98, 99, 100]
System.out.println(Arrays.toString(ret));
final var s = "☆◇△";

final var utf8 = s.getBytes("UTF-8");

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(utf8));
System.out.println(new String(utf8, "UTF-8")); // "☆◇△"

final var sjis = s.getBytes("Shift_JIS");

// [-127, -103, -127, -98, -127, -94]
System.out.println(Arrays.toString(sjis));
System.out.println(new String(sjis, "Shift_JIS")); // "☆◇△"

byte[] getBytes (Charset charset)

使用给定的字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。 (机器翻译)

final var ret = "abcd".getBytes(StandardCharsets.UTF_8);

// [97, 98, 99, 100]
System.out.println(Arrays.toString(ret));
final var s = "☆◇△";

final var utf8 = s.getBytes(StandardCharsets.UTF_8);

// [-30, -104, -122, -30, -105, -121, -30, -106, -77]
System.out.println(Arrays.toString(utf8));
System.out.println(new String(utf8, StandardCharsets.UTF_8)); // "☆◇△"

final var sjis = s.getBytes(Charset.forName("Shift_JIS"));

// [-127, -103, -127, -98, -127, -94]
System.out.println(Arrays.toString(sjis));
System.out.println(new String(sjis, Charset.forName("Shift_JIS"))); // "☆◇△"

void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)

将此字符串中的字符复制到目标字符数组中。 (机器翻译)

final var s = "abcde";

final var chars1 = new char[5];
s.getChars(0, 5, chars1, 0);
System.out.println(Arrays.toString(chars1)); // [a, b, c, d, e]

final var chars2 = new char[5];
s.getChars(1, 3, chars2, 0);
System.out.println(Arrays.toString(chars2)); // [b, c,  ,  ,  ]

final var chars3 = new char[5];
s.getChars(0, 2, chars3, 3);
System.out.println(Arrays.toString(chars3)); // [ ,  ,  , a, b]

int hashCode ()

返回此字符串的哈希码。 (机器翻译)

System.out.println("".hashCode());  // 0
System.out.println("a".hashCode()); // 97
System.out.println("ab".hashCode()); // 3105
System.out.println("123".hashCode()); // 48690
final var s = """
        abcdefghijklmnopqrstuvwxyz
        ABCDEFGHIJKLMNOPQRSTUVWXYZ
        123456790
        """;

System.out.println(s.hashCode()); // 573751039

String indent (int n)

根据 n 的值调整此字符串每行的缩进,并规范化行终止字符。 (机器翻译)

final var s = """
        Book
        Cake
        Tree
        Stone
        """;
// """
// Cake
// Tree
// Stone
// """
System.out.println(s);

// """
// Book
// Cake
// Tree
// Stone
// """
System.out.println(s.indent(0));

// """
//  Book
//  Cake
//  Tree
//  Stone
// """
System.out.println(s.indent(1));

// """
//   Book
//   Cake
//   Tree
//   Stone
// """
System.out.println(s.indent(2));

int indexOf (int ch)

返回此字符串中第一次出现指定字符的索引。 (机器翻译)

final var s = "abcXY";

System.out.println(s.indexOf('a')); // 0
System.out.println(s.indexOf('b')); // 1
System.out.println(s.indexOf('c')); // 2
System.out.println(s.indexOf('d')); // -1
System.out.println(s.indexOf('X')); // 3
System.out.println(s.indexOf('Y')); // 4
System.out.println(s.indexOf('Z')); // -1
final var s = "abcabc";
System.out.println(s.indexOf('a')); // 0
System.out.println(s.indexOf('b')); // 1
System.out.println(s.indexOf('c')); // 2
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.indexOf('a')); // 0

// 0x1f60a = 😊
System.out.println(s.indexOf(0x1f60a)); // 1

System.out.println(s.indexOf('b')); // 3

int indexOf (int ch, int fromIndex)

返回此字符串中第一次出现指定字符的索引,从指定索引开始搜索。 (机器翻译)

final var s = "abcabc";

System.out.println(s.indexOf('a', -1)); // 0
System.out.println(s.indexOf('a', 0)); // 0
System.out.println(s.indexOf('a', 1)); // 3
System.out.println(s.indexOf('a', 2)); // 3
System.out.println(s.indexOf('a', 3)); // 3
System.out.println(s.indexOf('a', 4)); // -1
System.out.println(s.indexOf('a', 5)); // -1
System.out.println(s.indexOf('a', 6)); // -1

System.out.println(s.indexOf('b', 0)); // 1
System.out.println(s.indexOf('b', 1)); // 1
System.out.println(s.indexOf('b', 2)); // 4
System.out.println(s.indexOf('b', 3)); // 4
System.out.println(s.indexOf('b', 4)); // 4
System.out.println(s.indexOf('b', 5)); // -1

System.out.println(s.indexOf('c', 0)); // 2
System.out.println(s.indexOf('c', 1)); // 2
System.out.println(s.indexOf('c', 2)); // 2
System.out.println(s.indexOf('c', 3)); // 5
System.out.println(s.indexOf('c', 4)); // 5
System.out.println(s.indexOf('c', 5)); // 5
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.indexOf('a', 0)); // 0
System.out.println(s.indexOf('a', 1)); // -1
System.out.println(s.indexOf('a', 2)); // -1
System.out.println(s.indexOf('a', 3)); // -1

// 0x1f60a = 😊
System.out.println(s.indexOf(0x1f60a, 0)); // 1
System.out.println(s.indexOf(0x1f60a, 1)); // 1
System.out.println(s.indexOf(0x1f60a, 2)); // -1
System.out.println(s.indexOf(0x1f60a, 3)); // -1

System.out.println(s.indexOf('b', 0)); // 3
System.out.println(s.indexOf('b', 1)); // 3
System.out.println(s.indexOf('b', 2)); // 3
System.out.println(s.indexOf('b', 3)); // 3
System.out.println(s.indexOf('b', 4)); // -1

int indexOf (int ch, int beginIndex, int endIndex)

返回此字符串中第一次出现指定字符的索引,从 beginIndex 处开始搜索并在 endIndex 之前停止。 (机器翻译)

Please see also : indexOf(int ch, int fromIndex)

final var s = "abcabc";

System.out.println(s.indexOf('b', 0, 6)); // 1
System.out.println(s.indexOf('b', 1, 6)); // 1
System.out.println(s.indexOf('b', 2, 6)); // 4
System.out.println(s.indexOf('b', 3, 6)); // 4
System.out.println(s.indexOf('b', 4, 6)); // 4
System.out.println(s.indexOf('b', 5, 6)); // -1
System.out.println(s.indexOf('b', 6, 6)); // -1

System.out.println(s.indexOf('b', 0, 0)); // -1
System.out.println(s.indexOf('b', 0, 1)); // -1
System.out.println(s.indexOf('b', 0, 2)); // 1
System.out.println(s.indexOf('b', 0, 3)); // 1
System.out.println(s.indexOf('b', 0, 4)); // 1
System.out.println(s.indexOf('b', 0, 5)); // 1
System.out.println(s.indexOf('b', 0, 6)); // 1

int indexOf (String str)

返回此字符串中第一次出现指定子字符串的索引。 (机器翻译)

final var s = "abcabc";

System.out.println(s.indexOf("a")); // 0
System.out.println(s.indexOf("ab")); // 0
System.out.println(s.indexOf("abc")); // 0
System.out.println(s.indexOf("bc")); // 1
System.out.println(s.indexOf("c")); // 2
System.out.println(s.indexOf("cab")); // 2

System.out.println(s.indexOf("abcd")); // -1
System.out.println(s.indexOf("ac")); // -1
System.out.println(s.indexOf("z")); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.indexOf("a")); // 0
System.out.println(s.indexOf(surrogate)); // 1
System.out.println(s.indexOf("b")); // 3

int indexOf (String str, int fromIndex)

从指定索引开始,返回此字符串中第一次出现指定子字符串的索引。 (机器翻译)

final var s = "abcabc";

System.out.println(s.indexOf("ab", -1)); // 0
System.out.println(s.indexOf("ab", 0)); // 0
System.out.println(s.indexOf("ab", 1)); // 3
System.out.println(s.indexOf("ab", 2)); // 3
System.out.println(s.indexOf("ab", 3)); // 3
System.out.println(s.indexOf("ab", 4)); // -1
System.out.println(s.indexOf("ab", 5)); // -1
System.out.println(s.indexOf("ab", 6)); // -1

System.out.println(s.indexOf("bc", 0)); // 1
System.out.println(s.indexOf("bc", 1)); // 1
System.out.println(s.indexOf("bc", 2)); // 4
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.indexOf("a", 0)); // 0
System.out.println(s.indexOf("a", 1)); // -1

System.out.println(s.indexOf(surrogate, 0)); // 1
System.out.println(s.indexOf(surrogate, 1)); // 1
System.out.println(s.indexOf(surrogate, 2)); // -1
System.out.println(s.indexOf(surrogate, 3)); // -1

System.out.println(s.indexOf("b", 0)); // 3
System.out.println(s.indexOf("b", 1)); // 3
System.out.println(s.indexOf("b", 2)); // 3
System.out.println(s.indexOf("b", 3)); // 3
System.out.println(s.indexOf("b", 4)); // -1

int indexOf (String str, int beginIndex, int endIndex)

返回此字符串的指定索引范围内第一次出现指定子字符串的索引。 (机器翻译)

Please see also : indexOf(String str, int fromIndex)

final var s = "abcabc";

System.out.println(s.indexOf("bc", 0, 6)); // 1
System.out.println(s.indexOf("bc", 1, 6)); // 1
System.out.println(s.indexOf("bc", 2, 6)); // 4
System.out.println(s.indexOf("bc", 3, 6)); // 4
System.out.println(s.indexOf("bc", 4, 6)); // 4
System.out.println(s.indexOf("bc", 5, 6)); // -1
System.out.println(s.indexOf("bc", 6, 6)); // -1

System.out.println(s.indexOf("bc", 0, 0)); // -1
System.out.println(s.indexOf("bc", 0, 1)); // -1
System.out.println(s.indexOf("bc", 0, 2)); // -1
System.out.println(s.indexOf("bc", 0, 3)); // 1
System.out.println(s.indexOf("bc", 0, 4)); // 1
System.out.println(s.indexOf("bc", 0, 5)); // 1
System.out.println(s.indexOf("bc", 0, 6)); // 1

String intern ()

返回字符串对象的规范表示。 (机器翻译)

final var s1 = new String("abc");
final var s2 = new String("abc");

System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

final var ret1 = s1.intern();
System.out.println(ret1); // "abc"

final var ret2 = s2.intern();
System.out.println(ret2); // "abc"

System.out.println(ret1 == ret2); // true

boolean isBlank ()

如果字符串为空或者仅包含空格代码点,则返回 true,否则返回 false。 (机器翻译)

System.out.println("".isBlank()); // true
System.out.println(" ".isBlank()); // true
System.out.println("     ".isBlank()); // true
System.out.println("\t".isBlank()); // true
System.out.println("\n".isBlank()); // true

System.out.println("abc".isBlank()); // false
System.out.println("a b c".isBlank()); // false

boolean isEmpty ()

当且仅当 length() 为 0 时才返回 true。 (机器翻译)

System.out.println("".isEmpty()); // true
System.out.println(" ".isEmpty()); // false
System.out.println("\n".isEmpty()); // false
System.out.println("\0".isEmpty()); // false
System.out.println("abc".isEmpty()); // false

static String join (CharSequence delimiter, CharSequence... elements)

返回一个由 CharSequence 元素的副本与指定分隔符的副本连接在一起组成的新字符串。 (机器翻译)

System.out.println(String.join("-", "Java", "is", "cool")); // "Java-is-cool"
System.out.println(String.join("->", "A", "B", "C")); // "A->B->C"

System.out.println(String.join("-")); // ""
System.out.println(String.join("-", "", "a", "", "b")); // "-a--b"

static String join (CharSequence delimiter, Iterable<? extends CharSequence> elements)

返回一个由 CharSequence 元素的副本与指定分隔符的副本连接在一起组成的新字符串。 (机器翻译)

System.out.println(String.join("-", List.of("Java", "is", "cool"))); // "Java-is-cool"
System.out.println(String.join("->", List.of("A", "B", "C"))); // "A->B->C"

System.out.println(String.join("-", List.of())); // ""
System.out.println(String.join("-", List.of("", "a", "", "b"))); // "-a--b"

int lastIndexOf (int ch)

返回此字符串中最后一次出现的指定字符的索引。 (机器翻译)

final var s = "abcXY";

System.out.println(s.lastIndexOf('a')); // 0
System.out.println(s.lastIndexOf('b')); // 1
System.out.println(s.lastIndexOf('c')); // 2
System.out.println(s.lastIndexOf('d')); // -1
System.out.println(s.lastIndexOf('X')); // 3
System.out.println(s.lastIndexOf('Y')); // 4
System.out.println(s.lastIndexOf('Z')); // -1
final var s = "abcabc";
System.out.println(s.lastIndexOf('a')); // 3
System.out.println(s.lastIndexOf('b')); // 4
System.out.println(s.lastIndexOf('c')); // 5
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.lastIndexOf('a')); // 0

// 0x1f60a = 😊
System.out.println(s.lastIndexOf(0x1f60a)); // 1

System.out.println(s.lastIndexOf('b')); // 3

int lastIndexOf (int ch, int fromIndex)

返回此字符串中最后一次出现指定字符的索引,从指定索引开始向后搜索。 (机器翻译)

final var s = "abcabc";

System.out.println(s.lastIndexOf('a', 5)); // 3
System.out.println(s.lastIndexOf('a', 4)); // 3
System.out.println(s.lastIndexOf('a', 3)); // 3
System.out.println(s.lastIndexOf('a', 2)); // 0
System.out.println(s.lastIndexOf('a', 1)); // 0
System.out.println(s.lastIndexOf('a', 0)); // 0

System.out.println(s.lastIndexOf('b', 5)); // 4
System.out.println(s.lastIndexOf('b', 4)); // 4
System.out.println(s.lastIndexOf('b', 3)); // 1
System.out.println(s.lastIndexOf('b', 2)); // 1
System.out.println(s.lastIndexOf('b', 1)); // 1
System.out.println(s.lastIndexOf('b', 0)); // -1

System.out.println(s.lastIndexOf('c', 5)); // 5
System.out.println(s.lastIndexOf('c', 4)); // 2
System.out.println(s.lastIndexOf('c', 3)); // 2
System.out.println(s.lastIndexOf('c', 2)); // 2
System.out.println(s.lastIndexOf('c', 1)); // -1
System.out.println(s.lastIndexOf('c', 0)); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.lastIndexOf('a', 3)); // 0
System.out.println(s.lastIndexOf('a', 2)); // 0
System.out.println(s.lastIndexOf('a', 1)); // 0
System.out.println(s.lastIndexOf('a', 0)); // 0

// 0x1f60a = 😊
System.out.println(s.lastIndexOf(0x1f60a, 3)); // 1
System.out.println(s.lastIndexOf(0x1f60a, 2)); // 1
System.out.println(s.lastIndexOf(0x1f60a, 1)); // 1
System.out.println(s.lastIndexOf(0x1f60a, 0)); // -1

System.out.println(s.lastIndexOf('b', 4)); // 3
System.out.println(s.lastIndexOf('b', 3)); // 3
System.out.println(s.lastIndexOf('b', 2)); // -1
System.out.println(s.lastIndexOf('b', 1)); // -1
System.out.println(s.lastIndexOf('b', 0)); // -1

int lastIndexOf (String str)

返回此字符串中最后一次出现的指定子字符串的索引。 (机器翻译)

final var s = "abcabc";

System.out.println(s.lastIndexOf("a")); // 3
System.out.println(s.lastIndexOf("ab")); // 3
System.out.println(s.lastIndexOf("abc")); // 3
System.out.println(s.lastIndexOf("bc")); // 4
System.out.println(s.lastIndexOf("c")); // 5
System.out.println(s.lastIndexOf("cab")); // 2

System.out.println(s.lastIndexOf("abcd")); // -1
System.out.println(s.lastIndexOf("ac")); // -1
System.out.println(s.lastIndexOf("z")); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.lastIndexOf("a")); // 0
System.out.println(s.lastIndexOf(surrogate)); // 1
System.out.println(s.lastIndexOf("b")); // 3

int lastIndexOf (String str, int fromIndex)

返回此字符串中最后一次出现的指定子字符串的索引,从指定索引开始向后搜索。 (机器翻译)

final var s = "abcabc";

System.out.println(s.lastIndexOf("ab", 5)); // 3
System.out.println(s.lastIndexOf("ab", 4)); // 3
System.out.println(s.lastIndexOf("ab", 3)); // 3
System.out.println(s.lastIndexOf("ab", 2)); // 0
System.out.println(s.lastIndexOf("ab", 1)); // 0
System.out.println(s.lastIndexOf("ab", 0)); // 0

System.out.println(s.lastIndexOf("bc", 5)); // 4
System.out.println(s.lastIndexOf("bc", 4)); // 4
System.out.println(s.lastIndexOf("bc", 3)); // 1
System.out.println(s.lastIndexOf("bc", 2)); // 1
System.out.println(s.lastIndexOf("bc", 1)); // 1
System.out.println(s.lastIndexOf("bc", 0)); // -1

System.out.println(s.lastIndexOf("ca", 5)); // 2
System.out.println(s.lastIndexOf("ca", 4)); // 2
System.out.println(s.lastIndexOf("ca", 3)); // 2
System.out.println(s.lastIndexOf("ca", 2)); // 2
System.out.println(s.lastIndexOf("ca", 1)); // -1
System.out.println(s.lastIndexOf("ca", 0)); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.lastIndexOf("a", 3)); // 0
System.out.println(s.lastIndexOf("a", 2)); // 0
System.out.println(s.lastIndexOf("a", 1)); // 0
System.out.println(s.lastIndexOf("a", 0)); // 0

System.out.println(s.lastIndexOf(surrogate, 3)); // 1
System.out.println(s.lastIndexOf(surrogate, 2)); // 1
System.out.println(s.lastIndexOf(surrogate, 1)); // 1
System.out.println(s.lastIndexOf(surrogate, 0)); // -1

System.out.println(s.lastIndexOf("b", 3)); // 3
System.out.println(s.lastIndexOf("b", 2)); // -1
System.out.println(s.lastIndexOf("b", 1)); // -1
System.out.println(s.lastIndexOf("b", 0)); // -1

int length ()

返回此字符串的长度。 (机器翻译)

System.out.println("".length()); // 0
System.out.println("abc".length()); // 3
System.out.println("☆◇△▽".length()); // 4
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.length()); // 4
System.out.println(s.codePoints().count()); // 3

Stream<String> lines ()

返回从该字符串中提取的行流,由行终止符分隔。 (机器翻译)

final var s = """
        Book
        Cake
        Stone
        """;
final var ret = s.lines();
System.out.println(ret.toList()); // [Book, Cake, Stone]
final var s = """

        Book

        Cake

        Stone

        """;
final var ret = s.lines();
System.out.println(ret.toList()); // [, Book, , Cake, , Stone, ]
final var ret = "".lines();
System.out.println(ret.toList()); // []

boolean matches (String regex)

判断此字符串是否与给定的正则表达式匹配。 (机器翻译)

final var s = "java is cool";

System.out.println(s.matches("java")); // false
System.out.println(s.matches("java.*")); // true

System.out.println(s.matches(".*cool")); // true
final var s1 = "Book";
final var s2 = "Cake";
final var s3 = "Stone";

System.out.println(s1.matches("Book|Cake")); // true
System.out.println(s2.matches("Book|Cake")); // true
System.out.println(s3.matches("Book|Cake")); // false

int offsetByCodePoints (int index, int codePointOffset)

返回此字符串中与给定索引偏移 codePointOffset 代码点的索引。 (机器翻译)

final var s = "abc";

//s.offsetByCodePoints(0, -1); // IndexOutOfBoundsException
System.out.println(s.offsetByCodePoints(0, 0)); // 0
System.out.println(s.offsetByCodePoints(0, 1)); // 1
System.out.println(s.offsetByCodePoints(0, 2)); // 2
System.out.println(s.offsetByCodePoints(0, 3)); // 3

System.out.println(s.offsetByCodePoints(1, 0)); // 1
System.out.println(s.offsetByCodePoints(1, 1)); // 2
System.out.println(s.offsetByCodePoints(1, 2)); // 3
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = "a" + surrogate + "b";
System.out.println(s); // "a😊b"

System.out.println(s.offsetByCodePoints(0, 0)); // 0
System.out.println(s.offsetByCodePoints(0, 1)); // 1
System.out.println(s.offsetByCodePoints(0, 2)); // 3
System.out.println(s.offsetByCodePoints(0, 3)); // 4

boolean regionMatches (boolean ignoreCase, int toffset, String other, int ooffset, int len)

测试两个字符串区域是否相等。 (机器翻译)

final var s = "abcde";

// ignoreCase = false
System.out.println(s.regionMatches(false, 0, "abcde", 0, 5)); // true
System.out.println(s.regionMatches(false, 0, "bcdef", 0, 5)); // false
System.out.println(s.regionMatches(false, 0, "ABCDE", 0, 5)); // false

System.out.println(s.regionMatches(false, 0, "bcd", 0, 3)); // false
System.out.println(s.regionMatches(false, 1, "bcd", 0, 3)); // true
System.out.println(s.regionMatches(false, 2, "bcd", 0, 3)); // false

System.out.println(s.regionMatches(false, 0, "012abcde345", 0, 11)); // false
System.out.println(s.regionMatches(false, 0, "012abcde345", 0, 5)); // false
System.out.println(s.regionMatches(false, 0, "012abcde345", 3, 5)); // true

// ignoreCase = true
System.out.println(s.regionMatches(true, 0, "abcde", 0, 5)); // true
System.out.println(s.regionMatches(true, 0, "bcdef", 0, 5)); // false
System.out.println(s.regionMatches(true, 0, "ABCDE", 0, 5)); // true

boolean regionMatches (int toffset, String other, int ooffset, int len)

测试两个字符串区域是否相等。 (机器翻译)

final var s = "abcde";

System.out.println(s.regionMatches(0, "abcde", 0, 5)); // true
System.out.println(s.regionMatches(0, "bcdef", 0, 5)); // false
System.out.println(s.regionMatches(0, "ABCDE", 0, 5)); // false

System.out.println(s.regionMatches(0, "bcd", 0, 3)); // false
System.out.println(s.regionMatches(1, "bcd", 0, 3)); // true
System.out.println(s.regionMatches(2, "bcd", 0, 3)); // false

System.out.println(s.regionMatches(0, "012abcde345", 0, 11)); // false
System.out.println(s.regionMatches(0, "012abcde345", 0, 5)); // false
System.out.println(s.regionMatches(0, "012abcde345", 3, 5)); // true

String repeat (int count)

返回一个字符串,其值是该字符串重复 count 次的连接。 (机器翻译)

System.out.println("A".repeat(0)); // ""
System.out.println("A".repeat(1)); // "A"
System.out.println("A".repeat(2)); // "AA"
System.out.println("A".repeat(3)); // "AAA"

System.out.println("abc".repeat(0)); // ""
System.out.println("abc".repeat(1)); // "abc"
System.out.println("abc".repeat(2)); // "abcabc"

System.out.println("".repeat(0)); // ""
System.out.println("".repeat(1)); // ""

String replace (char oldChar, char newChar)

返回用 newChar 替换该字符串中所有出现的 oldChar 得到的字符串。 (机器翻译)

final var s = "abcd";

System.out.println(s.replace('a', 'X')); // "Xbcd"
System.out.println(s.replace('b', 'Y')); // "aYcd"
System.out.println(s.replace('c', 'Z')); // "abZd"
final var s = "aabbcc";

System.out.println(s.replace('a', 'X')); // "XXbbcc"
System.out.println(s.replace('b', 'Y')); // "aaYYcc"

String replace (CharSequence target, CharSequence replacement)

用指定的文字替换序列替换此字符串中与文字目标序列匹配的每个子字符串。 (机器翻译)

final var s = "abcd";

System.out.println(s.replace("a", "X")); // "Xbcd"
System.out.println(s.replace("abc", "XYZ")); // "XYZd"
System.out.println(s.replace("ab", "")); // "cd"
final var s = "abcabc";

System.out.println(s.replace("a", "X")); // "XbcXbc"
System.out.println(s.replace("bc", "YYZZ")); // "aYYZZaYYZZ"

String replaceAll (String regex, String replacement)

用给定的替换值替换此字符串中与给定正则表达式匹配的每个子字符串。 (机器翻译)

final var s = "javaAAABBB";

System.out.println(s.replaceAll("java.", "x")); // "xAABBB"
System.out.println(s.replaceAll("A.", "y")); // "javayyBB"
System.out.println(s.replaceAll(".", "z")); // "zzzzzzzzzz"

String replaceFirst (String regex, String replacement)

用给定的替换来替换此字符串中与给定正则表达式匹配的第一个子字符串。 (机器翻译)

final var s = "javaAAABBB";

System.out.println(s.replaceFirst("java.", "x")); // "xAABBB"
System.out.println(s.replaceFirst("A.", "y")); // "javayABBB"
System.out.println(s.replaceFirst(".", "z")); // "zavaAAABBB"

String resolveConstantDesc (MethodHandles.Lookup lookup)

将此实例解析为 ConstantDesc,其结果就是实例本身。 (机器翻译)

final var ret = "abcd".resolveConstantDesc(MethodHandles.lookup());
System.out.println(ret); // "abcd"

String[] split (String regex)

根据给定正则表达式的匹配项拆分此字符串。 (机器翻译)

final var s = "a-b-c";

System.out.println(Arrays.toString(s.split("-"))); // [a, b, c]
System.out.println(Arrays.toString(s.split("b"))); // [a-, -c]
System.out.println(Arrays.toString(s.split("X"))); // [a-b-c]
final var s = "boo:and:foo";

System.out.println(Arrays.toString(s.split(":"))); // [boo, and, foo]
System.out.println(Arrays.toString(s.split("o"))); // [b, , :and:f]
final var s = "a-b>c";

System.out.println(Arrays.toString(s.split("-"))); // [a, b>c]
System.out.println(Arrays.toString(s.split(">"))); // [a-b, c]
System.out.println(Arrays.toString(s.split("[->]"))); // [a, b, c]

String[] split (String regex, int limit)

根据给定正则表达式的匹配项拆分此字符串。 (机器翻译)

final var s = "a-b-c";

System.out.println(Arrays.toString(s.split("-", 0))); // [a, b, c]
System.out.println(Arrays.toString(s.split("-", 1))); // [a-b-c]
System.out.println(Arrays.toString(s.split("-", 2))); // [a, b-c]
System.out.println(Arrays.toString(s.split("-", 3))); // [a, b, c]
final var s = "boo:and:foo";

System.out.println(Arrays.toString(s.split(":", 2))); // [boo, and:foo]
System.out.println(Arrays.toString(s.split(":", 5))); // [boo, and, foo]
System.out.println(Arrays.toString(s.split(":", -2))); // [boo, and, foo]
System.out.println(Arrays.toString(s.split("o", 5))); // [b, , :and:f, , ]
System.out.println(Arrays.toString(s.split("o", -2))); // [b, , :and:f, , ]
System.out.println(Arrays.toString(s.split("o", 0))); // [b, , :and:f]
final var s = "a-b>c";

System.out.println(Arrays.toString(s.split("-", 1))); // [a-b>c]
System.out.println(Arrays.toString(s.split("-", 2))); // [a, b>c]
System.out.println(Arrays.toString(s.split("-", 3))); // [a, b>c]

System.out.println(Arrays.toString(s.split(">", 1))); // [a-b>c]
System.out.println(Arrays.toString(s.split(">", 2))); // [a-b, c]
System.out.println(Arrays.toString(s.split(">", 3))); // [a-b, c]

System.out.println(Arrays.toString(s.split("[->]", 1))); // [a-b>c]
System.out.println(Arrays.toString(s.split("[->]", 2))); // [a, b>c]
System.out.println(Arrays.toString(s.split("[->]", 3))); // [a, b, c]

String[] splitWithDelimiters (String regex, int limit)

根据给定正则表达式的匹配项拆分此字符串,并返回字符串和匹配的分隔符。 (机器翻译)

final var s = "a-b-c";

final var ret1 = s.splitWithDelimiters("-", 0);
System.out.println(Arrays.toString(ret1)); // [a, -, b, -, c]

final var ret2 = s.splitWithDelimiters("-", 1);
System.out.println(Arrays.toString(ret2)); // [a-b-c]

final var ret3 = s.splitWithDelimiters("-", 2);
System.out.println(Arrays.toString(ret3)); // [a, -, b-c]

final var ret4 = s.splitWithDelimiters("-", 3);
System.out.println(Arrays.toString(ret4)); // [a, -, b, -, c]
final var s = "boo:and:foo";

final var ret1 = s.splitWithDelimiters(":", 2);
System.out.println(Arrays.toString(ret1)); // [boo, :, and:foo]

final var ret2 = s.splitWithDelimiters(":", 5);
System.out.println(Arrays.toString(ret2)); // [boo, :, and, :, foo]

final var ret3 = s.splitWithDelimiters(":", -2);
System.out.println(Arrays.toString(ret3)); // [boo, :, and, :, foo]

final var ret4 = s.splitWithDelimiters("o", 5);
System.out.println(Arrays.toString(ret4)); // [b, o, , o, :and:f, o, , o, ]

final var ret5 = s.splitWithDelimiters("o", -2);
System.out.println(Arrays.toString(ret5)); // [b, o, , o, :and:f, o, , o, ]

final var ret6 = s.splitWithDelimiters("o", 0);
System.out.println(Arrays.toString(ret6)); // [b, o, , o, :and:f, o, , o]
final var s = "a-b>c";

final var ret1 = s.splitWithDelimiters("-", 1);
System.out.println(Arrays.toString(ret1)); // [a-b>c]

final var ret2 = s.splitWithDelimiters("-", 2);
System.out.println(Arrays.toString(ret2)); // [a, -, b>c]

final var ret3 = s.splitWithDelimiters("-", 3);
System.out.println(Arrays.toString(ret3)); // [a, -, b>c]

final var ret4 = s.splitWithDelimiters(">", 1);
System.out.println(Arrays.toString(ret4)); // [a-b>c]

final var ret5 = s.splitWithDelimiters(">", 2);
System.out.println(Arrays.toString(ret5)); // [a-b, >, c]

final var ret6 = s.splitWithDelimiters(">", 3);
System.out.println(Arrays.toString(ret6)); // [a-b, >, c]

final var ret7 = s.splitWithDelimiters("[->]", 1);
System.out.println(Arrays.toString(ret7)); // [a-b>c]

final var ret8 = s.splitWithDelimiters("[->]", 2);
System.out.println(Arrays.toString(ret8)); // [a, -, b>c]

final var ret9 = s.splitWithDelimiters("[->]", 3);
System.out.println(Arrays.toString(ret9)); // [a, -, b, >, c]

boolean startsWith (String prefix)

测试此字符串是否以指定的前缀开头。 (机器翻译)

final var s = "abc";

System.out.println(s.startsWith("a")); // true
System.out.println(s.startsWith("ab")); // true
System.out.println(s.startsWith("abc")); // true
System.out.println(s.startsWith("abcd")); // false
System.out.println(s.startsWith("b")); // false
System.out.println(s.startsWith("c")); // false

boolean startsWith (String prefix, int toffset)

测试此字符串从指定索引开始的子字符串是否以指定前缀开头。 (机器翻译)

final var s = "abc";

System.out.println(s.startsWith("a", 0)); // true
System.out.println(s.startsWith("a", 1)); // false
System.out.println(s.startsWith("a", 2)); // false

System.out.println(s.startsWith("ab", 0)); // true
System.out.println(s.startsWith("ab", 1)); // false
System.out.println(s.startsWith("ab", 2)); // false

System.out.println(s.startsWith("abc", 0)); // true
System.out.println(s.startsWith("abc", 1)); // false
System.out.println(s.startsWith("abc", 2)); // false

System.out.println(s.startsWith("b", 0)); // false
System.out.println(s.startsWith("b", 1)); // true
System.out.println(s.startsWith("b", 2)); // false

System.out.println(s.startsWith("bc", 0)); // false
System.out.println(s.startsWith("bc", 1)); // true
System.out.println(s.startsWith("bc", 2)); // false

System.out.println(s.startsWith("c", 0)); // false
System.out.println(s.startsWith("c", 1)); // false
System.out.println(s.startsWith("c", 2)); // true

String strip ()

返回一个字符串,其值为该字符串,并删除所有前导和尾随空格。 (机器翻译)

Please see also : trim()

final var s = " abcd ";
System.out.println(s); // " abcd "
System.out.println(s.length()); // 6

final var ret = s.strip();
System.out.println(ret); // "abcd"
System.out.println(ret.length()); // 4
// u3000 = IDEOGRAPHIC SPACE
final var s = "\u3000☆◇△\u3000";
System.out.println(s); // " ☆◇△ "
System.out.println(s.length()); // 5

final var ret = s.strip();
System.out.println(ret); // "☆◇△"
System.out.println(ret.length()); // 3
final var s = " \t\nABC \t\n";

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*")
        .replace("\t", "\\t")
        .replace("\n", "\\n");

// "*\t\nABC*\t\n"
System.out.println(func.apply(s));

final var ret = s.strip();

// "ABC"
System.out.println(func.apply(ret));
final var s = """
          <html>
            <body>
              <p>Hello</p>
            </body>
          </html>  \
        """;

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*");

// """
// **<html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>**
// """
System.out.println(func.apply(s));

final var ret = s.strip();

// """
// <html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>
// """
System.out.println(func.apply(ret));

String stripIndent ()

返回一个字符串,其值为该字符串,并删除每行开头和结尾的随机空格。 (机器翻译)

final var s = " abcd ";
System.out.println(s); // " abcd "
System.out.println(s.length()); // 6

final var ret = s.stripIndent();
System.out.println(ret); // "abcd"
System.out.println(ret.length()); // 4
final var s = " \t\nABC \t\n";

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*")
        .replace("\t", "\\t")
        .replace("\n", "\\n");

// "*\t\nABC*\t\n"
System.out.println(func.apply(s));

final var ret = s.stripIndent();

// "\nABC\n"
System.out.println(func.apply(ret));
final var s = """
          <html>
            <body>
              <p>Hello</p>
            </body>
          </html>  \
        """;

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*");

// """
// **<html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>**
// """
System.out.println(func.apply(s));

final var ret = s.stripIndent();

// """
// <html>
// **<body>
// ****<p>Hello</p>
// **</body>
// </html>
// """
System.out.println(func.apply(ret));

String stripLeading ()

返回一个字符串,其值为该字符串,但删除了所有前导空格。 (机器翻译)

final var s = " abcd ";
System.out.println(s); // " abcd "
System.out.println(s.length()); // 6

final var ret = s.stripLeading();
System.out.println(ret); // "abcd "
System.out.println(ret.length()); // 5
final var s = " \t\nABC \t\n";

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*")
        .replace("\t", "\\t")
        .replace("\n", "\\n");

// "*\t\nABC*\t\n"
System.out.println(func.apply(s));

final var ret = s.stripLeading();

// "ABC*\t\n"
System.out.println(func.apply(ret));
final var s = """
          <html>
            <body>
              <p>Hello</p>
            </body>
          </html>  \
        """;

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*");

// """
// **<html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>**
// """
System.out.println(func.apply(s));

final var ret = s.stripLeading();

// """
// <html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>**
// """
System.out.println(func.apply(ret));

String stripTrailing ()

返回一个字符串,其值为该字符串,但删除了所有尾随空格。 (机器翻译)

final var s = " abcd ";
System.out.println(s); // " abcd "
System.out.println(s.length()); // 6

final var ret = s.stripTrailing();
System.out.println(ret); // " abcd"
System.out.println(ret.length()); // 5
final var s = " \t\nABC \t\n";

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*")
        .replace("\t", "\\t")
        .replace("\n", "\\n");

// "*\t\nABC*\t\n"
System.out.println(func.apply(s));

final var ret = s.stripTrailing();

// "*\t\nABC"
System.out.println(func.apply(ret));
final var s = """
          <html>
            <body>
              <p>Hello</p>
            </body>
          </html>  \
        """;

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*");

// """
// **<html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>**
// """
System.out.println(func.apply(s));

final var ret = s.stripTrailing();

// """
// **<html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>
// """
System.out.println(func.apply(ret));

CharSequence subSequence (int beginIndex, int endIndex)

返回此序列的子序列的字符序列。 (机器翻译)

final var s = "abcd";

System.out.println(s.subSequence(0, 0)); // ""
System.out.println(s.subSequence(0, 1)); // "a"
System.out.println(s.subSequence(0, 2)); // "ab"
System.out.println(s.subSequence(0, 3)); // "abc"
System.out.println(s.subSequence(0, 4)); // "abcd"
//s.subSequence(0, 5); // IndexOutOfBoundsException

System.out.println(s.subSequence(1, 2)); // "b"
System.out.println(s.subSequence(2, 4)); // "cd"

String substring (int beginIndex)

返回该字符串的子字符串。 (机器翻译)

final var s = "abcd";

System.out.println(s.substring(0)); // "abcd"
System.out.println(s.substring(1)); // "bcd"
System.out.println(s.substring(2)); // "cd"
System.out.println(s.substring(3)); // "d"
System.out.println(s.substring(4)); // ""
//s.substring(5); // IndexOutOfBoundsException

String substring (int beginIndex, int endIndex)

返回该字符串的子字符串。 (机器翻译)

final var s = "abcd";

System.out.println(s.substring(0, 0)); // ""
System.out.println(s.substring(0, 1)); // "a"
System.out.println(s.substring(0, 2)); // "ab"
System.out.println(s.substring(0, 3)); // "abc"
System.out.println(s.substring(0, 4)); // "abcd"
//s.substring(0, 5); // IndexOutOfBoundsException

System.out.println(s.substring(1, 2)); // "b"
System.out.println(s.substring(2, 4)); // "cd"

char[] toCharArray ()

将此字符串转换为新的字符数组。 (机器翻译)

final var ret1 = "".toCharArray();
System.out.println(Arrays.toString(ret1)); // []

final var ret2 = "abcd".toCharArray();
System.out.println(Arrays.toString(ret2)); // [a, b, c, d]

final var ret3 = "☆◇△".toCharArray();
System.out.println(Arrays.toString(ret3)); // [☆, ◇, △]

String toLowerCase ()

使用默认语言环境的规则将此字符串中的所有字符转换为小写。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

System.out.println("".toLowerCase()); // ""
System.out.println("abcdef".toLowerCase()); // "abcdef"
System.out.println("ABCDEF".toLowerCase()); // "abcdef"
System.out.println("ΙΧΘΥΣ".toLowerCase()); // "ιχθυς"

String toLowerCase (Locale locale)

使用给定区域设置的规则将此字符串中的所有字符转换为小写。 (机器翻译)

final var s1 = "I"; // u0049
final var s2 = "İ"; // u0130

{
    final var en = Locale.ENGLISH;

    final var ret1 = s1.toLowerCase(en);
    System.out.println(ret1); // "i"

    final var list1 = ret1.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list1); // [69]

    final var ret2 = s2.toLowerCase(en);
    System.out.println(ret2); // "i̇ "

    final var list2 = ret2.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list2); // [69, 307]
}

{
    final var tr = Locale.of("tr");

    final var ret1 = s1.toLowerCase(tr);
    System.out.println(ret1); // "ı"

    final var list1 = ret1.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list1); // [131]

    final var ret2 = s2.toLowerCase(tr);
    System.out.println(ret2); // "i"

    final var list2 = ret2.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list2); // [69]
}

String toString ()

这个对象(已经是一个字符串!) (机器翻译)

final var ret1 = "abcd".toString();
System.out.println(ret1); // "abcd"

final var ret2 = "☆◇△".toString();
System.out.println(ret2); // "☆◇△"

String toUpperCase ()

使用默认语言环境的规则将此字符串中的所有字符转换为大写。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

System.out.println("".toUpperCase()); // ""
System.out.println("abcdef".toUpperCase()); // "ABCDEF"
System.out.println("ABCDEF".toUpperCase()); // "ABCDEF"
System.out.println("ιχθυς".toUpperCase()); // "ΙΧΘΥΣ"

String toUpperCase (Locale locale)

使用给定区域设置的规则将此字符串中的所有字符转换为大写。 (机器翻译)

final var s1 = "i"; // u0069
final var s2 = "ı"; // u0130

{
    final var en = Locale.ENGLISH;

    final var ret1 = s1.toUpperCase(en);
    System.out.println(ret1); // "I"

    final var list1 = ret1.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list1); // [49]

    final var ret2 = s2.toUpperCase(en);
    System.out.println(ret2); // "I"

    final var list2 = ret2.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list2); // [49]
}

{
    final var tr = Locale.of("tr");

    final var ret1 = s1.toUpperCase(tr);
    System.out.println(ret1); // "İ"

    final var list1 = ret1.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list1); // [130]

    final var ret2 = s2.toUpperCase(tr);
    System.out.println(ret2); // "I"

    final var list2 = ret2.codePoints().mapToObj(Integer::toHexString).toList();
    System.out.println(list2); // [49]
}

<R> R transform (Function<? super String,? extends R> f)

此方法允许将函数应用到该字符串。 (机器翻译)

final var s = "1234";

final var ret = s.transform(v -> Integer.parseInt(v) * 2);
System.out.println(ret); // 2468
final var s = "a";

final var ret = s.transform(v -> v.repeat(5).toUpperCase());
System.out.println(ret); // "AAAAA"

String translateEscapes ()

返回一个字符串,其值为该字符串,其中转义序列被翻译为字符串文字。 (机器翻译)

final var s = "aaaa\\nBBBB";

System.out.println(s); // "aaaa\nBBBB"

// """
// aaaa
// BBBB
// """
System.out.println(s.translateEscapes());

String trim ()

返回一个字符串,其值以此字符串为单位,删除所有前导和尾随空格,其中空格定义为代码点小于或等于“U+0020”(空格字符)的任何字符。 (机器翻译)

Please see also : strip()

final var s = " abcd ";
System.out.println(s); // " abcd "
System.out.println(s.length()); // 6

final var ret = s.trim();
System.out.println(ret); // "abcd"
System.out.println(ret.length()); // 4
// u3000 = IDEOGRAPHIC SPACE
final var s = "\u3000☆◇△\u3000";
System.out.println(s); // " ☆◇△ "
System.out.println(s.length()); // 5

final var ret = s.trim();
System.out.println(ret); // " ☆◇△ "
System.out.println(ret.length()); // 5
final var s = " \t\nABC \t\n";

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*")
        .replace("\t", "\\t")
        .replace("\n", "\\n");

// "*\t\nABC*\t\n"
System.out.println(func.apply(s));

final var ret = s.trim();

// "ABC"
System.out.println(ret);
final var s = """
          <html>
            <body>
              <p>Hello</p>
            </body>
          </html>  \
        """;

// The function for whitespace readability.
final Function<String, String> func = (v) -> v.replace(" ", "*");

// """
// **<html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>**
// """
System.out.println(func.apply(s));

final var ret = s.trim();

// """
// <html>
// ****<body>
// ******<p>Hello</p>
// ****</body>
// **</html>
// """
System.out.println(func.apply(ret));

static String valueOf (boolean b)

返回布尔参数的字符串表示形式。 (机器翻译)

final var ret1 = String.valueOf(true);
System.out.println(ret1); // "true"

final var ret2 = String.valueOf(false);
System.out.println(ret2); // "false"

static String valueOf (char c)

返回 char 参数的字符串表示形式。 (机器翻译)

final var ret1 = String.valueOf('a');
System.out.println(ret1); // "a"

final var ret2 = String.valueOf('△');
System.out.println(ret2); // "△"

static String valueOf (char[] data)

返回字符数组参数的字符串表示形式。 (机器翻译)

final char[] data = {'b', 'o', 'o', 'k'};

final var ret = String.valueOf(data);
System.out.println(ret); // "book"
final char[] data = {'☆', '◇', '△'};

final var ret = String.valueOf(data);
System.out.println(ret); // "☆◇△"

static String valueOf (char[] data, int offset, int count)

返回字符数组参数的特定子数组的字符串表示形式。 (机器翻译)

final char[] data = {'b', 'o', 'o', 'k'};

System.out.println(String.valueOf(data, 0, 1)); // "b"
System.out.println(String.valueOf(data, 0, 2)); // "bo"
System.out.println(String.valueOf(data, 0, 3)); // "boo"
System.out.println(String.valueOf(data, 0, 4)); // "book"

System.out.println(String.valueOf(data, 1, 3)); // "ook"
System.out.println(String.valueOf(data, 2, 2)); // "ok"
System.out.println(String.valueOf(data, 3, 1)); // "k"

static String valueOf (double d)

返回双精度参数的字符串表示形式。 (机器翻译)

final double d1 = 1.0;
final double d2 = 0.12345;
final double d3 = 1.23e10;

final var ret1 = String.valueOf(d1);
System.out.println(ret1); // "1.0"

final var ret2 = String.valueOf(d2);
System.out.println(ret2); // "0.12345"

final var ret3 = String.valueOf(d3);
System.out.println(ret3); // "1.23E10"

static String valueOf (float f)

返回浮点参数的字符串表示形式。 (机器翻译)

final float f1 = 1.0f;
final float f2 = 0.12345f;
final float f3 = 1.23e10f;

final var ret1 = String.valueOf(f1);
System.out.println(ret1); // "1.0"

final var ret2 = String.valueOf(f2);
System.out.println(ret2); // "0.12345"

final var ret3 = String.valueOf(f3);
System.out.println(ret3); // "1.23E10"

static String valueOf (int i)

返回 int 参数的字符串表示形式。 (机器翻译)

final int i1 = 0;
final int i2 = 1234;
final int i3 = -5678;

final var ret1 = String.valueOf(i1);
System.out.println(ret1); // "0"

final var ret2 = String.valueOf(i2);
System.out.println(ret2); // "1234"

final var ret3 = String.valueOf(i3);
System.out.println(ret3); // "-5678"

static String valueOf (long l)

返回长参数的字符串表示形式。 (机器翻译)

final long l1 = 0L;
final long l2 = 1234567890123456789L;
final long l3 = -5678901234L;

final var ret1 = String.valueOf(l1);
System.out.println(ret1); // "0"

final var ret2 = String.valueOf(l2);
System.out.println(ret2); // "1234567890123456789"

final var ret3 = String.valueOf(l3);
System.out.println(ret3); // "-5678901234"

static String valueOf (Object obj)

返回对象参数的字符串表示形式。 (机器翻译)

final var obj = LocalTime.of(14, 30, 59);

final var ret = String.valueOf(obj);
System.out.println(ret); // "14:30:59"
final var obj = URI.create("https://example.com/");

final var ret = String.valueOf(obj);
System.out.println(ret); // "https://example.com/"

相关文章

To top of page