UnicodeBlock Identifies Script Ranges
Classify characters with Character.UnicodeBlock so a validator can reject unexpected scripts instead of matching ad-hoc code point ranges.
Code
import java.lang.Character.UnicodeBlock;
import java.util.Set;
public class ScriptGate {
static final Set<UnicodeBlock> ALLOWED = Set.of(
UnicodeBlock.BASIC_LATIN,
UnicodeBlock.LATIN_1_SUPPLEMENT,
UnicodeBlock.GENERAL_PUNCTUATION);
static String review(String text) {
for (int i = 0; i < text.length(); ) {
int cp = text.codePointAt(i);
i += Character.charCount(cp);
UnicodeBlock block = UnicodeBlock.of(cp);
if (block == null || !ALLOWED.contains(block)) {
return "reject: U+" + Integer.toHexString(cp).toUpperCase()
+ " belongs to " + block;
}
}
return "accept: all characters in allowed blocks";
}
public static void main(String[] args) {
System.out.println(review("invoice #42"));
System.out.println(review("facture n°42"));
System.out.println(review("发票 #42"));
}
}How the snippet works
UnicodeBlock Identifies Script Ranges reduces to the class ScriptGate and the functions review and main.
The cycling part of this snippet is for (int i = 0; i < text.length(); ).
What to watch for
- Nothing arrives from a registry; the language ships every API used.
Using it
Entry signature: static String review(String text)
main calls 1 helper in sequence: review.
Key statements
return "accept: all characters in allowed blocks";
i += Character.charCount(cp);
return "reject: U+" + Integer.toHexString(cp).toUpperCase()
+ " belongs to " + block;
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.
FAQ
- How do you run UnicodeBlock Identifies Script Ranges?
- The runtime alone is enough: execute the file as a complete program.
Report a problem with this exampleOpens a pre-filled issue on GitHub.