Java : CharSequence 示例
CharSequence (Java SE 22 & JDK 22) 示例。
您将在大多数 CharSequence 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
// Prints one character at a time.
public void print(CharSequence cs) {
cs.codePoints().forEach(c -> {
System.out.printf("%c,", c);
});
System.out.println();
}
final var str = "abcd";
print(str);
final var sb = new StringBuilder("xyz");
print(sb);
sb.append("ABC");
print(sb);
// Result
// ↓
//a,b,c,d,
//x,y,z,
//x,y,z,A,B,C,
Methods
char charAt (int index)
final CharSequence cs = "abc";
//cs.charAt(-1); // IndexOutOfBoundsException
System.out.println(cs.charAt(0)); // a
System.out.println(cs.charAt(1)); // b
System.out.println(cs.charAt(2)); // c
//cs.charAt(3); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"
final CharSequence cs = surrogate + "ab";
System.out.println(cs); // "😊ab"
final var ret1 = cs.charAt(0);
System.out.println(ret1); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret1)); // d83d
final var ret2 = cs.charAt(1);
System.out.println(ret2); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret2)); // de0a
final var ret3 = cs.charAt(2);
System.out.println(ret3); // a
final var ret4 = cs.charAt(3);
System.out.println(ret4); // b
//cs.charAt(4); // IndexOutOfBoundsException
default IntStream chars ()
final CharSequence cs = "abcd";
cs.chars().forEach(c -> {
System.out.printf("%x : %c%n", c, c);
});
// Result
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"
final CharSequence cs = "ab" + surrogate;
System.out.println(cs); // "ab😊"
cs.chars().forEach(c -> {
System.out.printf("%x : %c%n", c, c);
});
// Result
// ↓
// "61 : a"
// "62 : b"
// "d83d : ?" <- Garbled characters.
// "de0a : ?" <- Garbled characters.
default IntStream codePoints ()
final CharSequence cs = "abcd";
cs.codePoints().forEach(cp -> {
System.out.printf("%x : %c%n", cp, cp);
});
// Result
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"
final CharSequence cs = "ab" + surrogate;
System.out.println(cs); // "ab😊"
cs.codePoints().forEach(cp -> {
System.out.printf("%x : %c%n", cp, cp);
});
// Result
// ↓
// "61 : a"
// "62 : b"
// "1f60a : 😊"
static int compare (CharSequence cs1, CharSequence cs2)
final CharSequence cs1 = "abcd";
final CharSequence cs2 = "xyz";
final CharSequence cs3 = "XYZ";
System.out.println(CharSequence.compare(cs1, cs2)); // -23
System.out.println(CharSequence.compare(cs1, cs3)); // 9
System.out.println(CharSequence.compare(cs2, cs3)); // 32
final CharSequence cs1 = "abcd";
final CharSequence cs2 = new StringBuilder("xyz");
final CharSequence cs3 = new StringBuilder("abcd");
System.out.println(CharSequence.compare(cs1, cs2)); // -23
System.out.println(CharSequence.compare(cs1, cs3)); // 0
System.out.println(CharSequence.compare(cs2, cs3)); // 23
default boolean isEmpty ()
final CharSequence cs1 = "";
final CharSequence cs2 = "abcd";
System.out.println(cs1.isEmpty()); // true
System.out.println(cs2.isEmpty()); // false
final var sb = new StringBuilder();
final CharSequence cs = sb;
System.out.println(cs); // ""
System.out.println(cs.isEmpty()); // true
sb.append("xyz");
System.out.println(cs); // "xyz"
System.out.println(cs.isEmpty()); // false
int length ()
final CharSequence cs1 = "";
final CharSequence cs2 = "abcd";
System.out.println(cs1.length()); // 0
System.out.println(cs2.length()); // 4
final var sb = new StringBuilder();
final CharSequence cs = sb;
System.out.println(cs); // ""
System.out.println(cs.length()); // 0
sb.append("xyz");
System.out.println(cs); // "xyz"
System.out.println(cs.length()); // 3
CharSequence subSequence (int start, int end)
final CharSequence cs = "abcd";
System.out.println(cs.subSequence(0, 0)); // ""
System.out.println(cs.subSequence(0, 1)); // "a"
System.out.println(cs.subSequence(0, 2)); // "ab"
System.out.println(cs.subSequence(0, 3)); // "abc"
System.out.println(cs.subSequence(0, 4)); // "abcd"
//cs.subSequence(0, 5); // IndexOutOfBoundsException
System.out.println(cs.subSequence(0, 4)); // "abcd"
System.out.println(cs.subSequence(1, 4)); // "bcd"
System.out.println(cs.subSequence(2, 4)); // "cd"
System.out.println(cs.subSequence(3, 4)); // "d"
System.out.println(cs.subSequence(4, 4)); // ""
String toString ()
final CharSequence cs1 = "abcd";
final var str1 = cs1.toString();
System.out.println(str1); // "abcd"
final CharSequence cs2 = new StringBuilder("xyz");
final var str2 = cs2.toString();
System.out.println(str2); // "xyz"