How to capitalize the first character of each word in a string

后端 未结 30 1371
情深已故
情深已故 2020-11-22 02:08

Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?

Examples:

  • jon
30条回答
  •  执念已碎
    2020-11-22 02:27

    I've written a small Class to capitalize all the words in a String.

    Optional multiple delimiters, each one with its behavior (capitalize before, after, or both, to handle cases like O'Brian);

    Optional Locale;

    Don't breaks with Surrogate Pairs.

    LIVE DEMO

    Output:

    ====================================
     SIMPLE USAGE
    ====================================
    Source: cApItAlIzE this string after WHITE SPACES
    Output: Capitalize This String After White Spaces
    
    ====================================
     SINGLE CUSTOM-DELIMITER USAGE
    ====================================
    Source: capitalize this string ONLY before'and''after'''APEX
    Output: Capitalize this string only beforE'AnD''AfteR'''Apex
    
    ====================================
     MULTIPLE CUSTOM-DELIMITER USAGE
    ====================================
    Source: capitalize this string AFTER SPACES, BEFORE'APEX, and #AFTER AND BEFORE# NUMBER SIGN (#)
    Output: Capitalize This String After Spaces, BeforE'apex, And #After And BeforE# Number Sign (#)
    
    ====================================
     SIMPLE USAGE WITH CUSTOM LOCALE
    ====================================
    Source: Uniforming the first and last vowels (different kind of 'i's) of the Turkish word D[İ]YARBAK[I]R (DİYARBAKIR) 
    Output: Uniforming The First And Last Vowels (different Kind Of 'i's) Of The Turkish Word D[i]yarbak[i]r (diyarbakir) 
    
    ====================================
     SIMPLE USAGE WITH A SURROGATE PAIR 
    ====================================
    Source: ab 

提交回复
热议问题