Beam
asked on
Problem converting String to Title Case
Hello people,
Please could someone slove this for me. I have a method that is passed a string from a JTextfield on my GUI. Now I want to take that string and convert the first char and any char after whitespace into Capitals.
So for example 'springfield house' would become 'Springfield House'.
Below is what I have but the compiler does not want like it one bit!!!!!
public void setAddress(String newAddress){
for (int count = 1; count < newAddress.length(); count++){
if (newAddress.charAt(x).equa ls(" ")) //If char equal a blank space
newAddress.ChartAt(x++).to UpperCase( );
else
count++; }
address = newAddress;} //The variable ;address ;(String)
Can anyone help. The more simple the answer the better!
Shame String has'nt got a 'toTitleCase' method!
Thanks
Beam
Please could someone slove this for me. I have a method that is passed a string from a JTextfield on my GUI. Now I want to take that string and convert the first char and any char after whitespace into Capitals.
So for example 'springfield house' would become 'Springfield House'.
Below is what I have but the compiler does not want like it one bit!!!!!
public void setAddress(String newAddress){
for (int count = 1; count < newAddress.length(); count++){
if (newAddress.charAt(x).equa
newAddress.ChartAt(x++).to
else
count++; }
address = newAddress;} //The variable ;address ;(String)
Can anyone help. The more simple the answer the better!
Shame String has'nt got a 'toTitleCase' method!
Thanks
Beam
try:
StringBuffer temp = new StringBuffer(newAddress);
for (int count = 0; count < temp.length(); count++)
{
char c = temp.chatAt(count);
if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c) )
{
temp.setCharAt(Character.t oUpperCase (c));
}
}
address = temp.toString();
StringBuffer temp = new StringBuffer(newAddress);
for (int count = 0; count < temp.length(); count++)
{
char c = temp.chatAt(count);
if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c)
{
temp.setCharAt(Character.t
}
}
address = temp.toString();
>>You might want to move the first char to upper case come what may...
If that is the case (not sure what the 'rules' are) then do
sb.setCharAt(0, Character.toUpperCase(sb.c harAt(0))) ;
before returning
If that is the case (not sure what the 'rules' are) then do
sb.setCharAt(0, Character.toUpperCase(sb.c
before returning
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Objects, I tried your version first but it throws the syntax error ' StringBuffer can not be applied to Char' for the line 'temp.setCharAt(Character. toUpperCas e(c));' ????
About to try yours CEHJ.
About to try yours CEHJ.
> but it throws the syntax error
That was a typo, it's fixed in the 2nd code I posted.
That was a typo, it's fixed in the 2nd code I posted.
ASKER
Sorry Objects just got ya last version and it works great.
Thanks for your time guys.
Thanks for your time guys.
I think CEHJ's will crash if you pass it a blank string, also wasn't sure if you wanted the leading and trailing spaces trimmed.
Change last line to:
address = temp.toString().trim();
if you do.
Change last line to:
address = temp.toString().trim();
if you do.
Happy we could help :)
>>I think CEHJ's will crash if you pass it a blank string
And yours will crash if you pass it a null String ;-)
And yours will crash if you pass it a null String ;-)
good point:
public void setAddress(String newAddress)
{
if (newAddress==null)
{
address = null; //or u might want to set it to blank
}
else
{
StringBuffer temp = new StringBuffer(newAddress);
for (int count = 0; count < temp.length(); count++)
{
char c = temp.charAt(count);
if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c) )
{
temp.setCharAt(count, Character.toUpperCase(c));
}
}
address = temp.toString();
}
}
public void setAddress(String newAddress)
{
if (newAddress==null)
{
address = null; //or u might want to set it to blank
}
else
{
StringBuffer temp = new StringBuffer(newAddress);
for (int count = 0; count < temp.length(); count++)
{
char c = temp.charAt(count);
if ((count==0 || temp.charAt(count-1)==' ') && !Character.isWhitespace(c)
{
temp.setCharAt(count, Character.toUpperCase(c));
}
}
address = temp.toString();
}
}
public static String toTitleCase(String s) {
StringBuffer sb = new StringBuffer(s.trim());
for(int i = 0;i < sb.length();i++) {
char c = sb.charAt(i);
if (c == ' ')
sb.setCharAt(i + 1, Character.toUpperCase(sb.c
}
return sb.toString();
}
You might want to move the first char to upper case come what may...