Re: TitleCasing a String - [Perfect Solution]
Re: TitleCasing a String - [Perfect Solution]
- Subject: Re: TitleCasing a String - [Perfect Solution]
- From: Michael Engelhart <email@hidden>
- Date: Tue, 6 Jan 2004 15:35:14 -0500
Just to confuse things up I've been successfully using this code for
about a year now without a hitch:
public static String toTitleCase(String s) {
char[] ca = s.toCharArray();
boolean changed = false;
boolean capitalise = true;
boolean firstCap = true;
for ( int i=0; i<ca.length; i++ ) {
char oldLetter = ca[i];
if ( oldLetter <= '/'
|| ':' <= oldLetter && oldLetter <= '?'
|| ']' <= oldLetter && oldLetter <= '`' ) {
capitalise = true;
} else {
char newLetter = capitalise ? Character.toUpperCase(oldLetter) :
Character.toLowerCase(oldLetter);
ca[i] = newLetter;
changed |= (newLetter != oldLetter);
capitalise = false;
firstCap = false;
} // end if
} // end for
if ( changed ) {
s = new String (ca);
}
return s;
}
I don't know if it's better, faster or cheaper then any of the other
ones. I found it on one of the public code sites out there. Works
great but I don't know if it's the "best" solution out there.
Mike
On Jan 6, 2004, at 3:15 PM, Jonathan Fleming wrote:
This is probably best, I'm sure I've got it right this time
Jonathan :^)
public static String capitalise(String oldStr) {
if (oldStr == null) return oldStr;
int len = oldStr.length();
if (len == 0) return oldStr;
StringBuffer buff = new StringBuffer(len);
boolean white = true;
for (int i = 0; i < len; i++) {
char c = oldStr.charAt(i);
if (Character.isWhitespace(c)) {
buff.append(c);
white = true;
} else if (white) {
buff.append(Character.toTitleCase(c));
white = false;
} else {
buff.append(c);
}
}
return buff.toString();
}
From: "Jonathan Fleming" <email@hidden>
To: email@hidden
CC: email@hidden, email@hidden
Subject: Re: TitleCasing a String - [Perfect Solution]
Date: Tue, 06 Jan 2004 20:01:39 +0000
OOPs, forgot to delete || len == 0
How's 'hat Alexander ?
Thanks
Jonathan :^)
public static String capitalise(String oldStr) {
if (oldStr == null) {
return oldStr;
}
int len = oldStr.length();
StringBuffer buff = new StringBuffer(len);
boolean white = true;
for (int i = 0; i < len; i++) {
char c = oldStr.charAt(i);
if (Character.isWhitespace(c)) {
buff.append(c);
white = true;
} else if (white) {
buff.append(Character.toTitleCase(c));
white = false;
} else {
buff.append(c);
}
}
return buff.toString();
}
From: Alexander Spohr <email@hidden>
To: "Jonathan Fleming" <email@hidden>
Subject: Re: TitleCasing a String - [Perfect Solution]
Date: Tue, 6 Jan 2004 15:37:27 +0100
that's the non-working version :)
atze
Am 06.01.2004 um 14:08 schrieb Jonathan Fleming:
Thanks guys,
absolutely perfect.
Jonathan :^)
From: Geoff Hopson <email@hidden>
To: Alexander Spohr <email@hidden>
CC: Jonathan Fleming <email@hidden>,
email@hidden
Subject: Re: TitleCasing a String - probably a java question
Date: Tue, 6 Jan 2004 11:00:07 +0000
public static String capitalise(String oldStr) {
int len = oldStr.length();
if (oldStr == null || len == 0) {
return oldStr;
}
StringBuffer buff = new StringBuffer(len);
boolean white = true;
for (int i = 0; i < len; i++) {
char c = oldStr.charAt(i);
if (Character.isWhitespace(c)) {
buff.append(c);
white = true;
} else if (white) {
buff.append(Character.toTitleCase(c));
white = false;
} else {
buff.append(c);
}
}
return buff.toString();
}
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today!
http://www.msn.co.uk/messenger
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.