Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regex to capitalize first letter of string

Reply
Thread Tools

Regex to capitalize first letter of string

 
 
jay
Guest
Posts: n/a
 
      02-25-2004
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
 
Reply With Quote
 
 
 
 
Marc Dzaebel
Guest
Posts: n/a
 
      02-25-2004
"jay" <> schrieb im Newsbeitrag
news: om...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capitalize first word of a string? Rails List Ruby 4 06-05-2009 05:37 PM
Function to capitalize string Rick C++ 6 11-11-2007 11:19 AM
Capitalize First Letter Exlcuding ( ) and / gil@ambry.com Javascript 3 06-05-2005 03:06 PM
How do I Capitalize the first letter? nntp Perl Misc 7 10-11-2004 01:11 PM
Capitalize regex Marek Mand Javascript 7 05-05-2004 12:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57