Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-29-2004, 04:07 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 04-29-2004, 05:13 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-29-2004, 05:19 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 04-29-2004, 05:28 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-29-2004, 05:39 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 04-29-2004, 05:39 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-29-2004, 05:45 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 05-03-2004, 07:58 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
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
sde is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
Upper case string to lower case gamehead200 Standard C, C++ 12 10-15-2004 03:24 AM
My game Dodo Standard C, C++ 2 06-27-2004 10:48 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 11:00 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting