Unicode-correct title case in Java

前端 未结 3 661
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 09:41

I\'ve been looking through all StackOverflow in the bazillion of questions about capitalizing a word in Java, and none of them seem to care the least about internationalizat

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 10:44

    Like you, I was unable to find a suitable method in the core Java API.

    However, there does seem to be a locale-sensitive string-title-case method (UCharacter#toTitleCase) in the ICU library.


    Looking at the source for the relevant ICU methods (UCharacter#toTitleCase and UCaseProps#toUpperOrTitle), there don't seem to be many locale-specific special cases for title-casing, so you might be able to get away with the following:

    1. Find the first cased character in the string.
    2. If it has a title-case form distinct from its upper-case form, use that.
    3. Otherwise, perform a locale-sensitive upper-case on that first character and its combining characters.
    4. Perform a locale-sensitive lower-case on the rest of the string.
    5. If the locale is Dutch and the first cased character is an "I" followed by a "j", upper-case the "j".

提交回复
热议问题