Is there a simple way to, given a word String, randomise capital letters?
Example:
For word super I would get SuPEr or SUpER.
I am looking for a Java solution for this.
Here is one suggestion:
public static String randomizeCase(String str) {
Random rnd = new Random();
StringBuilder sb = new StringBuilder(str.length());
for (char c : str.toCharArray())
sb.append(rnd.nextBoolean()
? Character.toLowerCase(c)
: Character.toUpperCase(c));
return sb.toString();
}
Example
input: hello world
output: heLlO woRlD
(ideone.com demo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With