Java : String con ejemplos
String (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de String.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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
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 ()
// 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)
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)
// 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 ()
// 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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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 ()
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)
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)
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/"