|
 |
|
 |
04-29-2004, 04:07 PM
|
#1 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,470
|
First Letter Upper Case
I made this function to set the first character of each word in a string to upper case.
When i use it, it returns an empty string.
Anything obvious I'm doing wrong here?
Code:
public static String ucFirst(String strText)
{
if(strText.length() == 0)
return strText;
strText = strText.toLowerCase();
String[] array;
if( strText.indexOf(" ") < 0 )
{
array = new String[] { strText };
}
else
{
array = strText.split("[ ]");
}
String strOut = "";
for(int i=0;i>array.length;i++)
{
char[] word = array[i].toCharArray();
word[0] = Character.toUpperCase(word[0]);
array[i] = word.toString();
strOut += " " + array[i];
}
return strOut.trim();
}
__________________
Mike
|
|
|
04-29-2004, 05:13 PM
|
#2 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,148
|
Would never be able to tell you came to Java from C . . .
The main problem is your for loop is wrong. You're checking to see it i > array.length, when it should be i < array.length.
Anyways, use StringTokenizer to do this, it'll save you a lot of work.
And don't use += with Strings, use StringBuffer. String concatination is very inefficent. Try concating a million strings together, then try it with StringBuffer, and you'll notice a big difference.
|
|
|
04-29-2004, 05:19 PM
|
#3 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,470
|
omg!! i coded all day without a lunch,.. even still i can't believe i overlooked that!
that's funny you bring up the string concatination bit.
i made a display class for some things like printing tables from a customer array, .. however, i was using += to concat the string and after about a thousand records or so, it was entirely too slow.
so i ended up coding everything directly into the jsp page, and using out.println instead of making a big string.
i'll be looking into the stringTokenizer.. thanks a lot!
__________________
Mike
|
|
|
04-29-2004, 05:28 PM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,148
|
Here's how I would do it:
Code:
public static String ucFirst(String strText){
if(strText == null || strText.length() == 0){
return "";
}
StringTokenizer toks = new StringTokenizer(strText);
StringBuffer output = new StringBuffer();
while(toks.hasMoreTokens()){
String word = toks.nextToken();
output.append(word.substring(0,1).toUpperCase());
if(word.length() > 1){
output.append(word.substring(1).toLowerCase());
}
output.append(" ");
}
return output.toString();
}
Hopefully you're not worried about instances where you might want a capitalize letter in the middle of the word, like DuPont.
|
|
|
04-29-2004, 05:39 PM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,470
|
cool thanks a lot!
basically, all names and addresses in our db2 database are capitolized. the purpose of this is just to make things a little easier to read on the computer in a web application.
i'll just be using it for names and address'
i will have to apollogize to teh DuPon and DaVincis' of the world. lol
__________________
Mike
|
|
|
04-29-2004, 05:39 PM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,148
|
BTW, I've been coding in shell script all day. I really love Java, so feel free to ask for advice on any code snippets you might have, it'd be a pleasant diversion.
|
|
|
04-29-2004, 05:45 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,470
|
eh great thanks! i'm coded out .. and i have the rest of the week off .. i gotta program some php though for my side business.
thanks again for all the help, .. i'm sure i'll keep coming up with lots more next week.
__________________
Mike
|
|
|
05-03-2004, 07:58 AM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,470
|
ok, and it's back to work here. just wanted to thank you for that fuction. it works perfectly.
i tried my old one and it was creating all sorts of weird characters.
StringTokenizer is nice =) Thanks!
__________________
Mike
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:00 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|