Convert a String to Modified Camel Case in Java or Title Case as is otherwise called
123,364
Solution 1
I used the below to solve this problem.
import org.apache.commons.lang.StringUtils;
StringUtils.capitalize(MyString);
Thanks to Ted Hopp for rightly pointing out that the question should have been TITLE CASE instead of modified CAMEL CASE.
Camel Case is usually without spaces between words.
Solution 2
You can easily write the method to do that :
public static String toCamelCase(final String init) {
if (init == null)
return null;
final StringBuilder ret = new StringBuilder(init.length());
for (final String word : init.split(" ")) {
if (!word.isEmpty()) {
ret.append(Character.toUpperCase(word.charAt(0)));
ret.append(word.substring(1).toLowerCase());
}
if (!(ret.length() == init.length()))
ret.append(" ");
}
return ret.toString();
}
Solution 3
From commons-lang3
org.apache.commons.lang3.text.WordUtils.capitalizeFully(String str)
Solution 4
static String toCamelCase(String s){
String[] parts = s.split(" ");
String camelCaseString = "";
for (String part : parts){
if(part!=null && part.trim().length()>0)
camelCaseString = camelCaseString + toProperCase(part);
else
camelCaseString=camelCaseString+part+" ";
}
return camelCaseString;
}
static String toProperCase(String s) {
String temp=s.trim();
String spaces="";
if(temp.length()!=s.length())
{
int startCharIndex=s.charAt(temp.indexOf(0));
spaces=s.substring(0,startCharIndex);
}
temp=temp.substring(0, 1).toUpperCase() +
spaces+temp.substring(1).toLowerCase()+" ";
return temp;
}
public static void main(String[] args) {
String string="HI tHiS is SomE Statement";
System.out.println(toCamelCase(string));
}
Related videos on Youtube
Author by
takrishna
Web Developer (Javascript, HTML/CSS), Chrome Extension, Android
Updated on July 09, 2022Comments
-
takrishna almost 2 years
I want to convert any string to modified Camel case or Title case using some predefined libraries than writing my own functions.
For example
"HI tHiS is SomE Statement"
to"Hi This Is Some Statement"
Regex or any standard library will help me.
I found certain library functions in eclipse like
STRING.toCamelCase();
is there any such thing existing?-
Ted Hopp almost 11 yearsPerhaps you mean title case? "Camel case" usually refers to things like "HiThisIsSomeStatement" (no delimiters between the words).
-
takrishna almost 11 yearsSorry I didn't know about title case. Thanks Ted Hopp. I want Title Case
-
-
Florent Bayle almost 11 yearsThis code throws an
StringIndexOutOfBoundsException
if there are two consecutive white-spaces in theString
(or if theString
is empty). It throws aNullPointerException
if theString
is null. -
NFE almost 11 years@FlorentBayle oh let see updated answer!!
-
fge almost 11 yearsInstead of
word.substring(0, 1).toUpperCase()
you could doCharacter.toUpperCase(word.charAt(0))
-
Admin almost 6 yearsnot working in android studio java
-
takrishna about 5 yearsHave you imported the required Apache package - definitely this utility is not tied to any IDE - it is a Java utility
-
aswzen over 4 yearsDeprecated, replaced by org.apache.commons.text
-
tez almost 2 years@fge This is quite old comment but I want to know your thought process behind this. Can you please elaborate the difference between the two.