codePoints Stream Walks Each Character
Stream a string as int code points with String.codePoints, so per-character logic counts one emoji as a single value instead of two surrogate units.
Code
public class PassportCheck {
public static void main(String[] args) {
String plate = "AB🚀12";
// chars() 按 UTF-16 代码单元遍历:🚀 被拆成两个孤立的代理单元,
// 数出来是 6;codePoints() 还原成 int 码点,数出来是 5
System.out.println("char units: " + plate.chars().count());
System.out.println("code points: " + plate.codePoints().count());
// 按码点分派:字母、数字、其他各走各的分支
long letters = plate.codePoints().filter(Character::isLetter).count();
long digits = plate.codePoints().filter(Character::isDigit).count();
long others = plate.codePoints().count() - letters - digits;
System.out.println("letters=" + letters + " digits=" + digits + " others=" + others);
// 收集回字符串:appendCodePoint 保持码点完整,不会截断代理对
String digitsOnly = plate.codePoints()
.filter(Character::isDigit)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
System.out.println("digits only: " + digitsOnly);
}
}How it works
codePoints Stream Walks Each Character comes down to the class PassportCheck and the function main.
Caveats
- No dependencies section needed; the standard library is the whole story.
Using it
The primary signature is public static void main(String[] args)
The run prints char units:, code points:, letters= — each prefix names the step that produced it.
Key statements
long letters = plate.codePoints().filter(Character::isLetter).count();
long digits = plate.codePoints().filter(Character::isDigit).count();
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
Interactive Tools for This Topic
Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Text ReverserReverse text by characters, words, or lines, counting Unicode code points so emoji stay intact.
FAQ
- What does codePoints Stream Walks Each Character print when run?
- Running the file directly is enough — expect labeled output lines such as `char units:`, `code points:`, `letters=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.