huzefaq
asked on
how to capitalize first letter and put space between a string
Hi
I get the state names from a link in this format
alabama
northCarolina etc
I capitalize the first letter using this code
String capitalize_chapterName = chapter_name.length()>1? chapter_name.substring(0,1 ).toUpperC ase() + chapter_name.substring(1). toLowerCas e() : chapter_name.toUpperCase() ;
it works fine for one word sattes
for northCarolina i need it to be North Carolina
Any help will be greatly appreciated
I get the state names from a link in this format
alabama
northCarolina etc
I capitalize the first letter using this code
String capitalize_chapterName = chapter_name.length()>1? chapter_name.substring(0,1
it works fine for one word sattes
for northCarolina i need it to be North Carolina
Any help will be greatly appreciated
try:
s = s.replaceAll("([A-Z])", " {1}");
s = s.replaceAll("([A-Z])", " {1}");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I havn't compiled...chk if it works for u
str="northCarolina"
StringBuffer = new (str);
if (Character.isLowerCase(str buf.charAt ( 0)))
{
Character.toUpperCase(strb uf.charAt( 0))
}
for ( i = 0; i < strbuf.length(); i++ )
{
ch = strbuf.charAt( i );
if (Character.isUpperCase(ch) )
{
strbuf.insert(i , " ");
}
}
str=strbuf.toString();
System.out.println(strbuf) ;
str="northCarolina"
StringBuffer = new (str);
if (Character.isLowerCase(str
{
Character.toUpperCase(strb
}
for ( i = 0; i < strbuf.length(); i++ )
{
ch = strbuf.charAt( i );
if (Character.isUpperCase(ch)
{
strbuf.insert(i , " ");
}
}
str=strbuf.toString();
System.out.println(strbuf)
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html
create a string buffer and then loop through the string, character by character, and if the letter is uppercase, then add a space and the letter to the buffer, otherwise just add the letter.
using the character class, you can even perform more fine tunning like removing whitespaces, determining if the character is a letter or a digit, etc.