Regex to capitalize first letter of string

Discussion in 'Java' started by jay, Feb 25, 2004.

  1. jay

    jay Guest

    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
    jay, Feb 25, 2004
    #1
    1. Advertising

  2. jay

    Marc Dzaebel Guest

    "jay" <> schrieb im Newsbeitrag
    news:...
    > 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
    Marc Dzaebel, Feb 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    741
    Reinhold Birkenfeld
    Jul 6, 2004
  2. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    485
    Tony Meyer
    Jul 6, 2004
  3. Rails List
    Replies:
    4
    Views:
    202
    Rails List
    Jun 5, 2009
  4. nntp
    Replies:
    7
    Views:
    168
    Anno Siegel
    Oct 11, 2004
  5. Replies:
    3
    Views:
    152
    Dr John Stockton
    Jun 5, 2005
Loading...

Share This Page