2
\$\begingroup\$

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...

\$\endgroup\$
1
3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$ – AdHominem Apr 30 '16 at 11:49
  • \$\begingroup\$ 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 \$\endgroup\$ – janos Apr 30 '16 at 11:54
  • \$\begingroup\$ @AdHominem I combined some ideas from the other answer and comments for a slightly more compact alternative, see the update. \$\endgroup\$ – janos Apr 30 '16 at 12:03
3
\$\begingroup\$

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".

Addendum:

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.

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$ – Simon Forsberg Apr 30 '16 at 11:30
  • \$\begingroup\$ The isLetter(...) call is redundant. toUpperCase will only process letters anyway @SimonForsberg \$\endgroup\$ – rolfl Apr 30 '16 at 11:44
  • \$\begingroup\$ @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 \$\endgroup\$ – Simon Forsberg Apr 30 '16 at 11:46
  • 1
    \$\begingroup\$ And that, is of course a remarkably good point. \$\endgroup\$ – rolfl Apr 30 '16 at 11:48
  • \$\begingroup\$ 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) \$\endgroup\$ – BrainFRZ Apr 30 '16 at 11:49
2
\$\begingroup\$

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();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ As much as I appreciate a functional approach, this solution provides wrong results because it simply alternates every other letter. \$\endgroup\$ – AdHominem May 3 '16 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.