Uploaded image for project: 'Commons Text'
  1. Commons Text
  2. TEXT-47

WordUtils.capitalize() can't handle 1:M conversions

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • 1.x
    • None

    Description

      Some case conversions are not 1:1, for instance the German letter ß, which is normally capitalised to 'SS'.

      // Failing test
      assertEquals("SS", WordUtils.capitalize("\u00DF"));
      

      If we were using upper case and not title case, a solution such as the following would work:

      public static String capitalize(final String str, final char... delimiters) {
          final int delimLen = delimiters == null ? -1 : delimiters.length;
          if (StringUtils.isEmpty(str) || delimLen == 0) {
              return str;
          }
          final StringBuffer buffer = new StringBuffer(str.length());
          final char[] chars = str.toCharArray();
          boolean capitalizeNext = true;
          for (int i = 0; i < chars.length; i++) {
              final char ch = chars[i];
              if (isDelimiter(ch, delimiters)) {
                  capitalizeNext = true;
                  buffer.append(ch);
              } else if (capitalizeNext) {
                  // Use ENGLISH locale to be backwards compatible with previous releases, which
                  // used Character.toUpperCase()
                  buffer.append(String.valueOf(ch).toUpperCase(Locale.ENGLISH));
                  capitalizeNext = false;
              } else {
                  buffer.append(ch);
              }
          }
          return buffer.toString();
      }
      

      ... but as we use title case, we can't use the String class to convert for us.

      Attachments

        Activity

          People

            Unassigned Unassigned
            dmjones500 Duncan Jones
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: