Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Check to see if char is unicode

Hello,

I'm currently having some trouble detecting if a character is a specific Unicode.

My assignment is to code a Character Reader in which it reads an input file that contains a string and I'm supposed to print how many of the Unicode characters in the file are ASCII characters, Special characters, Characters that can be encoded in ISO 8859-1, Greek letters (Α-Ω, α-ω), and Characters that are not in the basic multilingual plane

For example:
String: The circumference of a circle is 2×π×r². ℍ is the set of quaternions and ? is the set of octonions.

Occurrences:
ASCII: 94
Special: 2
ISO 8859-1: 97
Greek: 1
notInBmp: 1

My problem is how do I detect a certain unicode occurrence between ASCII, special, greek letters, etc etc.

Would love some help on this.

Thanks
[Image: DTc14yu.png]
Code
String str = "2×π×r²☯";
        CharsetEncoder iso8859_1 = Charset.forName("ISO-8859-1").newEncoder();
        for(int i=0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if((int)ch < 128)
                System.out.println("ASCII at " + i);
            else if(iso8859_1.canEncode(ch))
                System.out.println("ISO-8859-1 at " + i);
            else if((int)ch >= 0x370 && (int)ch <= 0x3FF)
                System.out.println("Greek at " + i);
            else
                System.out.println("Unicode at " + i);
        }

Output
Code
ASCII at 0
ISO-8859-1 at 1
Greek at 2
ISO-8859-1 at 3
ASCII at 4
ISO-8859-1 at 5
Unicode at 6

To detect an ASCII char simply check if it is lower than 128, if you want the extended set check if it is lower than 256
To check a specific charset use Charset.forName("CHARSET").newEncoder().canEncode(char)
To check a specific unicode range see here http://www.unicode.org/Public/UCD/latest/ucd/Blocks.txt
Else it is simply part of the unicode charset
(Feb 6, 2016 02:41 AM)RedWaters Wrote:  
Code
String str = "2×π×r²☯";
        CharsetEncoder iso8859_1 = Charset.forName("ISO-8859-1").newEncoder();
        for(int i=0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if((int)ch < 128)
                System.out.println("ASCII at " + i);
            else if(iso8859_1.canEncode(ch))
                System.out.println("ISO-8859-1 at " + i);
            else if((int)ch >= 0x370 && (int)ch <= 0x3FF)
                System.out.println("Greek at " + i);
            else
                System.out.println("Unicode at " + i);
        }

Output
Code
ASCII at 0
ISO-8859-1 at 1
Greek at 2
ISO-8859-1 at 3
ASCII at 4
ISO-8859-1 at 5
Unicode at 6

To detect an ASCII char simply check if it is lower than 128, if you want the extended set check if it is lower than 256
To check a specific charset use Charset.forName("CHARSET").newEncoder().canEncode(char)
To check a specific unicode range see here http://www.unicode.org/Public/UCD/latest/ucd/Blocks.txt
Else it is simply part of the unicode charset
Charset.forName("CHARSET").newEncoder().canEncode(char)

this changed my life

thanks
[Image: DTc14yu.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by maryousJul 24, 2022 03:35 PM 0 161 Jul 24, 2022 03:35 PM
Last Post: maryous
[C] by KeatFeb 21, 2019 08:28 PM 3 181 Feb 21, 2019 08:28 PM
Last Post: 0xDEAD10CC
[C] by freenexusNov 5, 2018 04:10 AM 3 214 Nov 5, 2018 04:10 AM
Last Post: RascalPascal