34

Possible Duplicate:
How to upper case every first letter of word in a string?
Most efficient way to make the first character of a String lower case?

I want to convert the first letter of a string to upper case. I am attempting to use replaceFirst() as described in JavaDocs, but I have no idea what is meant by regular expression.

Here is the code I have tried so far:

public static String cap1stChar(String userIdea)
{
    String betterIdea, userIdeaUC;
    char char1;
    userIdeaUC = userIdea.toUpperCase();
    char1 = userIdeaUC.charAt(0);
    betterIdea = userIdea.replaceFirst(char1); 
    return betterIdea;
}//end cap1stChar

The compiler error is that the argument lists differ in lengths. I presume that is because the regex is missing, however I don't know what that is exactly.

Community
  • 1
  • 1
Matt B
  • 1,653
  • 2
  • 15
  • 20

7 Answers7

74

Regular Expressions (abbreviated "regex" or "reg-ex") is a string that defines a search pattern.

What replaceFirst() does is it uses the regular expression provided in the parameters and replaces the first result from the search with whatever you pass in as the other parameter.

What you want to do is convert the string to an array using the String class' charAt() method, and then use Character.toUpperCase() to change the character to upper case (obviously). Your code would look like this:

char first = Character.toUpperCase(userIdea.charAt(0));
betterIdea = first + userIdea.substring(1);

Or, if you feel comfortable with more complex, one-lined java code:

betterIdea = Character.toUpperCase(userIdea.charAt(0)) + userIdea.substring(1);

Both of these do the same thing, which is converting the first character of userIdea to an upper case character.

Jon Egeland
  • 11,466
  • 7
  • 45
  • 62
  • 2
    `userIdea.toCharArray()[0]` is an expensive way of doing `userIdea.charAt(0)` – Peter Lawrey Jul 13 '12 at 08:16
  • @PeterLawrey I don't know how I forgot about that. Thanks. – Jon Egeland Jul 13 '12 at 16:48
  • StringBuilder class is great companion suppose name is String than code => if(name.length()>0){ StringBuilder stringBuilder = new StringBuilder(name); if(stringBuilder.length()==1){ name= String.valueOf(Character.toUpperCase(stringBuilder.charAt(0))); }else{ name= String.valueOf(Character.toUpperCase(stringBuilder.charAt(0)))+stringBuilder.substring(1); } } – Rajesh Prasad Yadav Nov 22 '18 at 15:55
41

Or you can do

s = Character.toUpperCase(s.charAt(0)) + s.substring(1); 
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
6
public static String cap1stChar(String userIdea)
{
    char[] stringArray = userIdea.toCharArray();
    stringArray[0] = Character.toUpperCase(stringArray[0]);
    return userIdea = new String(stringArray);
}
Pramod Kumar
  • 7,552
  • 5
  • 26
  • 37
4

Comilation error is due arguments are not properly provided, replaceFirst accepts regx as initial arg. [a-z]{1} will match string of simple alpha characters of length 1.

Try this.

betterIdea = userIdea.replaceFirst("[a-z]{1}", userIdea.substring(0,1).toUpperCase())
Isuru
  • 6,867
  • 8
  • 26
  • 37
4
String toCamelCase(String string) {
    StringBuffer sb = new StringBuffer(string);
    sb.replace(0, 1, string.substring(0, 1).toUpperCase());
    return sb.toString();

}
Pranav Kevadiya
  • 479
  • 1
  • 5
  • 15
3
userIdeaUC = userIdea.substring(0, 1).toUpperCase() + userIdea.length() > 1 ? userIdea.substring(1) : "";

or

userIdeaUC = userIdea.substring(0, 1).toUpperCase();
if(userIdea.length() > 1)
   userIdeaUC += userIdea.substring(1);
juergen d
  • 186,950
  • 30
  • 261
  • 325
0

For completeness, if you wanted to use replaceFirst, try this:

public static String cap1stChar(String userIdea)
{
  String betterIdea = userIdea;
  if (userIdea.length() > 0)
  {
    String first = userIdea.substring(0,1);
    betterIdea = userIdea.replaceFirst(first, first.toUpperCase());
  }
  return betterIdea;
}//end cap1stChar
azhrei
  • 2,223
  • 14
  • 18