Wiki Markup |
---|
According to the Java API \[[API 2006|AA. Java References#APIBibliography#API 06]\], class {{Character}} documentation (Unicode Character Representations) |
...
Wiki Markup |
---|
This noncompliant code example attempts to trim leading letters from the {{string}}. It fails to accomplish this task because {{Character.isLetter()}} does not work for supplementary and combining characters \[[Hornig 2007|AA. Java References#HornigBibliography#Hornig 07]\] (sic). |
Code Block | ||
---|---|---|
| ||
// Fails for supplementary or combining characters public static String trim_bad1(String string) { char ch; for (int i = 0; i < string.length(); i += 1) { ch = string.charAt(i); if (!Character.isLetter(ch)) { break; } } return string.substring(i); } |
...
Wiki Markup |
---|
This noncompliant code example ameliorates the problem by using the {{String.codePointAt()}} method, which accepts an {{int}} argument. This works for supplementary characters but not for combining characters \[[Hornig 2007|AA. JavaBibliography#Hornig References#Hornig 07]\] (sic). |
Code Block | ||
---|---|---|
| ||
// Fails for combining characters public static String trim_bad2(String string) { int ch; for (int i = 0; i < string.length(); i += Character.charCount(ch)) { int ch = string.codePointAt(i); if (!Character.isLetter(ch)) { break; } } return string.substring(i); } |
...
Wiki Markup |
---|
This compliant solution works for both supplementary and combining characters \[[Hornig 2007|AA. Java References#HornigBibliography#Hornig 07]\] (sic). According to the Java API \[[API 2006|AA. JavaBibliography#API References#API 06]\], class {{java.text.BreakIterator}} documentation |
...
Wiki Markup |
---|
\[[API 2006|AA. Java References#APIBibliography#API 06]\] Classes {{Character}} and {{BreakIterator}} \[[Hornig 2007|AA. Java References#HornigBibliography#Hornig 07]\] Problem areas: Characters |
...