-4

I need to make a method that makes capital letters only at the beginning of words, is something easy maybe but I haven't been able to make it work so far. Cause the use case I have tested it are diff, Cause I don't have only the case where the first character is a string it can be a number or something else.

The result I want :
test => Test
test start now => Test start now.
2test start now => 2test start now
_test => _test
-test => -test
Ilia Tapia
  • 629
  • 3
  • 10
  • 23
  • 4
    user `str.toUpperCase()` https://stackoverflow.com/questions/3904579/how-to-capitalize-the-first-letter-of-a-string-in-java – The Scientific Method May 21 '19 at 08:23
  • Please provide the code you attempted to write and discuss how it's not working. – Ramzah Rehman May 21 '19 at 08:24
  • as mentioned, you can use toUpperCase(). forexample, substring the first letter and substring the rest of the word. Make the first letter capital then concatenate the whole thing – Mike_Jr May 21 '19 at 08:26
  • 1
    Possible duplicate of [How to capitalize the first letter of a String in Java?](https://stackoverflow.com/questions/3904579/how-to-capitalize-the-first-letter-of-a-string-in-java) – SirFartALot May 21 '19 at 08:33

3 Answers3

1
  1. Check if the first char is Alphabetic
  2. Change the letter to upper case
    List<String> values = Arrays.asList("test => Test",
            "test start now => Test start now.",
            "2test start now => 2test start now",
            "_test => _test",
            "-test => -test");

    @Test
    public void firstLetterUpperCase() {
        // Iterate the values
        values.stream()
                // Check if the first char is Alphabetic
                .filter(text -> Character.isAlphabetic(text.charAt(0)))
                // Capital first letter and print
                .forEach(text -> System.out.println(text.substring(0, 1).toUpperCase() + text.substring(1)));
    }

Output

Test => Test
Test start now => Test start now.
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
0
String text = "text";
String capitalize = text.substring(0, 1).toUpperCase() + text.substring(1);

It takes first character, and does toUpperCase(), then add the rest of the string.

If first character is not a letter, toUpperCase() won't change it.

  • "-".toUpperCase() is "-"
  • "1".toUpperCase() is "1"
  • "a".toUpperCase() is "A"
  • "A".toUpperCase() is "A"
SirFartALot
  • 1,215
  • 5
  • 25
Said
  • 689
  • 6
  • 20
  • 3
    code dump with no explanation is unhelpful. add an explanation how come this works for cases that do not start with letter – Sharon Ben Asher May 21 '19 at 08:25
  • 1
    It takes first character, and does toUpperCase(), then add the rest of the string. If first character is not a letter, toUpperCase() won't change it. "-".toUpperCase() is "-" "1".toUpperCase() is "1" "a".toUpperCase() is "A" "A".toUpperCase() is "A" – Said May 21 '19 at 08:31
0

You could also use org.apache.commons.text.WordUtils.capitalize​(String str, char... delimiters) Javadoc from apache.commons-text to captalize words.

Against your request, it capitalizes all words in a string/sentence not only the first character.

To uppercase the first character of a String use org.apache.commons.lang3.StringUtils .capitalize(String str) (Javadoc) from the apache.commons-lang package.

SirFartALot
  • 1,215
  • 5
  • 25