Regex to capitalize first letter of string

J

jay

Hi All,

I cannot seem to figure out what should be a simple regex:

given a String x, how do I make the first letter of x capitalized?

Thanks in advance,
Jay
 
M

Marc Dzaebel

jay said:
I cannot seem to figure out what should be a simple regex:
given a String x, how do I make the first letter of x capitalized?

public static String up1(String s) {
return (s.length()>0)? Character.toUpperCase(s.charAt(0))+s.substring(1) :
s;
}

You don't need a regular expression for this task. They are used for
matching/parsing issues. However, e.g. "[A-Z]" matches uppercase letters
while [a-z] matches lowercase letters.

E.g.
....
Pattern p = Pattern.compile("[a-z]*");
Matcher m = p.matcher("lUUU");
....
m.matches() --> true

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top