Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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. Bibliography#Hornig 07]\] (sic).

Code Block
bgColor#FFcccc
// 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. Bibliography#Hornig 07]\] (sic).

Code Block
bgColor#FFcccc
// 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. Bibliography#Hornig 07]\] (sic). According to the Java API \[[API 2006|AA. Bibliography#API 06]\], class {{java.text.BreakIterator}} documentation

...