View Single Post
Old 04-29-2004, 04:07 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
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 offline   Reply With Quote