1

Let's say we have a string in java:

String str = "ALL CAPS MATE";

How can I change it so that it looks like this but for any word:

String result = "All Caps Mate";

Is there a String method similar to .toUpperCase() or .toLowerCase()? If there is, I can't seem to find it.

I would appreciate a string method solution.

Thanks.

Aeternus
  • 346
  • 4
  • 15
  • http://stackoverflow.com/questions/3696441/converting-a-char-to-uppercase. Loop through the string and upper/lower each character – Matthew Fisher Aug 08 '16 at 02:34
  • There is not such a method in String. – Grim Aug 08 '16 at 02:36
  • There is not a builtin, so consider making your own method. make it all lower case, split the string on the spaces into an array, split each element into an array of chars, access the first element of each element in the overall array and make it uppercase, then join everything back together – Bald Bantha Aug 08 '16 at 02:36
  • This is NOT a duplicate. The OP requires something quite different from the OP of that other question; and the answers to that other question don't apply here. @Andreas – Dawood ibn Kareem Aug 08 '16 at 03:38
  • As David Wallace stated, this is NOT a duplicate. It is similar, I'll admit that. The other questions don't have strings with all caps. They have mix of lower and upper case and I wanted a solution that applied to strings with all upper case since I'm going to be gathering a lot of data that comes in all caps which is a format I don't want. Hence I asked this question. – Aeternus Aug 08 '16 at 05:35

3 Answers3

3

The apache libraries contain some methods to handle this as mentioned by B'bek Shakya and a comment, but if you are looking for a straight native Java solution you would do this as Bald Banta mentioned in the comments.

Here is a code example using streams:

public static void main(String[] args) {
    String input = "fOo bar";
    String transformed = Arrays.stream(input.toLowerCase()
                                            .split(" "))
                               .map(String::toCharArray)
                               .map(arr -> {
                                   arr[0] = Character.toUpperCase(arr[0]);
                                   return arr;
                               })
                               .map(String::valueOf)
                               .collect(Collectors.joining(" "));
    System.out.println(transformed);
}

And the same thing using a more common loop idiom:

public static void main(String[] args) {
    String input = "fOo bar";
    String[] words = input.toLowerCase().split(" ");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < words.length; i++) {
        char[] word = words[i].toCharArray();
        word[0] = Character.toUpperCase(word[0]);
        if (i != 0) {
            sb.append(" ");
        }
        sb.append(String.valueOf(word));
    }
    System.out.println(sb.toString());
}
Aaron Davis
  • 1,731
  • 10
  • 13
2

I think this would solve your problem by using apache library for more information

org.apache.commons.lang3.text.WordUtils

 String result= WordUtils.capitalizeFully(str);

for more complex one look at this link

and i think your question is already answer here

Community
  • 1
  • 1
Bibek Shakya
  • 1,233
  • 2
  • 23
  • 45
1

The WordUtils class from org.apache.commons.lang3.text has a static capitalizeFully method, which does what you require. You'll need the commons lang3 jar in your classpath.

import org.apache.commons.lang3.text.WordUtils;

WordUtils.capitalizeFully(input);
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110