简体   繁体   中英

Convert a String to title case in android

Before marking my question as duplicate , notice that there is a fundamental misunderstanding in that thread as to what "title case" means. In the english language, Title Case is not the same as having each word capitalized. In a title, we should

  • Capitalize nouns, pronouns, adjectives, verbs, adverbs, and subordinate conjunctions.
  • Lowercase articles (a, an, the), coordinating conjunctions, and prepositions.

Since the similar question I am pointing to is also a bit dated, I was wondering if anyone is aware of any facility in android to convert a string to a title case ? Or is the only option to roll out my own class?

For some background if you care, I am building an app that has some user generated contents, in addition to expert generated contents. I want my app to look intelligent and professional. So for the EditText I enable textAutoComplete|textAutoCorrect . But in addition, I want the title to look like titles, not just strings that have been typed by random users.

It is very late answer but may be can help others.

public static String changeStringCase(String s) {

final String DELIMITERS = " '-/"; 

StringBuilder sb = new StringBuilder();
boolean capNext = true;

for (char c : s.toCharArray()) {
    c = (capNext)
            ? Character.toUpperCase(c)
            : Character.toLowerCase(c);
    sb.append(c);
    capNext = (DELIMITERS.indexOf((int) c) >= 0); 
}
return sb.toString(); }

Output will be

  1. cHANge caSE --> Change Case
  2. CHANGE CASE --> Change Case
  3. change case --> Change Case

etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM