0

I have a thought:(might too completed)

  1. I wonder if I can substring(1) then use .toUpperCase() for the string
  2. and after that use getLength() to have the length then .toLowerCase() the rest in the string

Is there a easier way to do this? So I can have the value stored in "Xxxx" format. Thank you.

For example: No matter user input the value as "hELlo" or "HEllO" , the system always store the value as "Hello". That's may explain my question.

Lenic
  • 33
  • 7

3 Answers3

1

I wrote a small utility for my self. It might help you. I was sure that my string would always have alphabets in it so did not care about any other characters. you might want to modify as per your needs.

int length = "yourstring".length();/// get the length of the string
        String camelCase = removeCharacters.substring(0, 1).toUpperCase();// upper case the first alphabet
        camelCase = camelCase + "yourstring".substring(1, length).toLowerCase();// lowercase all other alphabets
  • Why are you reinventing the wheel? Apache already comes with this out of the box, and camelCase is different from what OP asked for in his question. – hd1 Feb 25 '16 at 21:43
0

Substring has a one-arg version and a two-arg version. The second argument is non-inclusive. So, the first bit of the string you want is

myString.subString(0, 1)

and the second bit is

myString.subString(1)

You can then call the toUpper and toLower methods on the results, and concatenate them.

Burleigh Bear
  • 3,274
  • 22
  • 32
0

Yes, the Apache Commons-Lang project has a WordUtils class with a capitalize method, as in the following example:

WordUtils.capitalize("i am FINE") yields: "I Am FINE"
hd1
  • 33,938
  • 5
  • 80
  • 91
  • If I would like to use this method. What package should I import? I'm using blueJay, it says "org.apache.commons.lang3.text" not exist. (import org.apache.commons.lang3.text.WordUtils;) – Lenic Feb 26 '16 at 06:43
  • Include the commons-lang3 jar on your CLASSPATH and import org.apache.commons.lang3.text.WordUtils at the top of your file. – hd1 Feb 26 '16 at 06:49