# Convert string to mixed case

This method converts a string to mixed case, id est mixedCase("hello, world!") -> "HeLlO, wOrLd!".

// Any input string is lower case already
private static String mixedCase(String input) {

// getAsCharacterList() simply turns a string into a List of Characters.
List<Character> charactersAsList = getAsCharacterList(input);
StringBuilder result = new StringBuilder();
int index = 0;

for (Character character : charactersAsList) {
if (Character.isLetter(character)) {
if (index % 2 == 0) {
result.append(Character.toUpperCase(character));
} else {
result.append(character);
}
index++;
} else {
result.append(character);
}
}

return result.toString();
}


It works, but admittedly it looks terrible and I have difficulties making it more concise. I would have preferred to use streams if I didn't had to reference indices...

Is there a more concise way to write this Java string converter?

Sure. No need for the intermediary charactersAsList variable, nor the getAsCharacterList method.

Since you know in advance the final length of the StringBuilder, it's good to pass that to the constructor when initializing.

// input is already lowercase
private static String mixedCase(String input) {
StringBuilder result = new StringBuilder(input.length());
int index = 0;

for (char character : input.toCharArray()) {
if (Character.isLetter(character)) {
if (index % 2 == 0) {
result.append(Character.toUpperCase(character));
} else {
result.append(character);
}
index++;
} else {
result.append(character);
}
}

return result.toString();
}


Borrowing from the idea of @BrainFRZ to set the already lowercased input in StringBuilder, and from @ferada's comment to use a boolean to decide to uppercase or not, here's another variation that has some advantages:

private static String mixedCase(String input) {
StringBuilder result = new StringBuilder(input);
int index = 0;
boolean toUpper = true;

for (char character : input.toCharArray()) {
if (Character.isLetter(character)) {
if (toUpper) {
character = Character.toUpperCase(character);
}
toUpper = !toUpper;
}
result.setCharAt(index, character);
index++;
}

return result.toString();
}


This takes advantage of the input being already lowercased, and naturally letting StringBuilder to use that. With some of the conditions eliminated, this version is slightly more compact.

• The method arguments will already be lower cased, i clarified that in my OP. What is the advantage about initializing the StringBuilder with a known length? Apr 30 '16 at 11:49
• Ok, that change in the description invalidated a paragraph in my answer, but fine, I updated it accordingly. StringBuilder dynamically increases its storage as needed, which can lead to reallocation of memory and array copies. If you know the final size in advance, StringBuilder can allocate the right amount memory from the start, and avoid resizing and performance loss Apr 30 '16 at 11:54
• @AdHominem I combined some ideas from the other answer and comments for a slightly more compact alternative, see the update. Apr 30 '16 at 12:03

There's actually a really easy way of doing this with a StringBuilder! All you need to do is set up a new StringBuilder and use its build-in setCharAt method.

public class StringTest {
private static String mixedCase(String input) {
StringBuilder sb = new StringBuilder(input.toLowerCase());

for (int i = 0; i < input.length(); i += 2) {
sb.setCharAt(i, Character.toUpperCase(input.charAt(i)));
}

return sb.toString();
}

public static void main(String[] args) {
System.out.println(mixedCase("testing")); //--> TeStInG
}
}


This way you don't have to do any fun with modulo operations, collections, or any of that stuff. If you want to have it mixed the other way, you can start the for-loop at i = 1 for "tEsTiNg".

As discussed in the comments, this solution only applies if you want the case to alternate every other letter based on its position in the string as opposed to alternating the case of every other letter ignoring non-letters.

• This doesn't take into the account the Character.isLetter(character) in condition in the original code, and so the output of this will be different. Consider the input string hello world for example. Apr 30 '16 at 11:30
• The isLetter(...) call is redundant. toUpperCase will only process letters anyway @SimonForsberg Apr 30 '16 at 11:44
• @rolfl That's not what I was thinking of, in the OP's code, index++ is only performed if the character is a letter.Which means that in the OP's code hello world will become HeLlO wOrLd while this code makes it HeLlO WoRlD Apr 30 '16 at 11:46
• And that, is of course a remarkably good point. Apr 30 '16 at 11:48
• That's true, I was only testing with the "Hello, world!" which gave his expected output and I wasn't thinking that it was because ", " is two characters. (I also forgot to have input.toLowerCase() in my answer, and I've updated that part) Apr 30 '16 at 11:49

Here's another variation with stream:

public class Converter {
public static void main(String[] args) {
System.out.println(mixedCase("hello, world"));
}

public static String mixedCase(String s) {
return IntStream.range(0, s.length())
.mapToObj(i -> i % 2 == 0 ? Character.toUpperCase(str.charAt(i)) : str.charAt(i))
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
}

• As much as I appreciate a functional approach, this solution provides wrong results because it simply alternates every other letter. May 3 '16 at 15:09