The number of characters in a Java string

Possible duplicate:
Java: line length when using unicode overline to display square roots?

How to get the number of Unicode characters in a string?

Given char[] Thai characters:

 [อ, ภ, ิ, ช, า, ต, ิ] 

This is displayed in String as: อภิ ชาติ

String.length() returns 7. I understand that there are (technically) 7 characters, but I need a method that will return me 5. This is the exact number of spaces of characters displayed on the screen.

+4
source share
3 answers

It seems you just don't want to treat Unicode labels as separate characters;

 static boolean isMark(char ch) { int type = Character.getType(ch); return type == Character.NON_SPACING_MARK || type == Character.ENCLOSING_MARK || type == Character.COMBINING_SPACING_MARK; } 

which can be used as:

 String olle = "อภิชาติ"; int count = 0; for(int i=0; i<olle.length(); i++) { if(!isMark(olle.charAt(i))) count++; } System.out.println(count); 

and returns '5'.

+5
source

You can adapt the solution posted to this question here:

Unicode for string conversion in Java

Separating the "#" character and counting the remaining characters in the string.

+1
source

You can use java.text.BreakIterator to find the gaps between the graphemes ("visual symbols") and count them. Here is an example:

 import java.text.BreakIterator; .. int graphemeLength(String str) { BreakIterator iter = BreakIterator.getCharacterInstance(); iter.setText(str); int count = 0; while (iter.next() != BreakIterator.DONE) count++; return count; } 

Now graphemeLength("อภิชาติ") will return 5.

0
source

Source: https://habr.com/ru/post/1437929/


All Articles