Hello,
I have a string taken as input from user. I just want to check if a particular character within that string, is part of Standard English character set or not.
How do I code the IF condition for this purpose?
Regards,
Arvind.
Hello,
I have a string taken as input from user. I just want to check if a particular character within that string, is part of Standard English character set or not.
How do I code the IF condition for this purpose?
Regards,
Arvind.
The phrase "standard English character set" is ambiguous — it can mean (a) only the letters A–Z / a–z, (b) printable ASCII (space + visible punctuation + digits + letters), or (c) the whole US-ASCII range (0..127). Pick the meaning that matches your task before choosing a check. Replies from and point you toward the simple letter-only check, and / offered whitelist approaches; those work for small, fixed sets but get awkward if you need locale/Unicode correctness.
For a robust, encoding-based test (is this character representable in US-ASCII) use a CharsetEncoder rather than hand-maintained lists. Example pattern (handles full code points safely):
CharsetEncoder ascii = Charset.forName("US-ASCII").newEncoder();
int cp = str.codePointAt(i);
String ch = new String(Character.toChars(cp));
if (ascii.canEncode(ch)) { /* representable in US-ASCII */ } CharsetEncoder.canEncode(...) is the intended API for this. (docs.oracle.com)
If you want “English letters” but need to avoid letters from other scripts, combine a Unicode-block check with a letter test:
int cp = str.codePointAt(i);
if (Character.UnicodeBlock.of(cp) == Character.UnicodeBlock.BASIC_LATIN
&& Character.isLetter(cp)) { /* Latin letter A–Z a–z */ } UnicodeBlock and Character.isLetter are the APIs to use here (and remember to work with code points, not plain char). (docs.oracle.com) (docs.oracle.com)
If you want to treat accented letters like plain English (e.g., "é" ≈ "e"), normalize then strip diacritics before testing:
String norm = Normalizer.normalize(ch, Normalizer.Form.NFD);
String base = norm.replaceAll("\\p{M}+", "");
// then test base for ASCII or Basic Latin Normalizer helps decompose combined characters for that purpose. (docs.oracle.com)
Quick guidance: for the simplest word-only check pick a lightweight letter test; for transport/compatibility checks use CharsetEncoder; for language-aware matching use Unicode-aware APIs and Normalizer.
Jump to Post— peter_budo 2,532Forget if use for loop. Put all characters you want to check upon in a List, then with help of for loop go through word and check if List contains() that character.
Forget if use for loop. Put all characters you want to check upon in a List, then with help of for loop go through word and check if List contains() that character.
Another way to check a single char.
Make it a String and use indexOf with a long String containing all the valid characters.
Do you mean a-z, A-Z ? If so, just check char between 'a' and 'z' OR between 'A' and 'Z'.
using .charAt(position ); you will get the i character in the String .
about checking is std English character you can check simply if the character fall between 'a' and 'z'
char c='a';
if (c>='a'&&c<='z')
System.out.println("Valid");
else
System.out.println("not Valid"); i hope i helped .
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.