Java ThreadLocalRandom randomCase(String input)

Here you can find the source of randomCase(String input)

Description

Flips the case of some characters in the input (about 50% of characters).

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static String randomCase(String input) 

Method Source Code

//package com.java2s;

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**/*  w ww.j  a  v  a 2 s.co m*/
     * Flips the case of some characters in the input (about 50% of characters).
     *
     * @param input
     * @return
     */
    public static String randomCase(String input) {
        if (input != null) {
            StringBuilder output = new StringBuilder();
            for (int i = 0; i < input.length(); ++i) {
                char c = input.charAt(i);
                if (Character.isAlphabetic(c) && ThreadLocalRandom.current().nextDouble() < 0.5) {
                    if (Character.isLowerCase(c))
                        output.append(Character.toUpperCase(c));
                    else if (Character.isUpperCase(c))
                        output.append(Character.toLowerCase(c));
                } else
                    output.append(c);
            }
            return output.toString();
        } else
            return null;

    }
}

Related

  1. random(int min, int max)
  2. random(T[] array)
  3. randomAge()
  4. randomAlphabetic(int length)
  5. randomBoundedInclusiveInt(int start, int end)
  6. randomChar()
  7. randomDelay()
  8. randomDouble(final double min, final double max)
  9. randomElement(Collection c, int n, boolean unique)