Java Random String randomlyRecaseCodePoints(Random random, String str)

Here you can find the source of randomlyRecaseCodePoints(Random random, String str)

Description

Randomly upcases, downcases, or leaves intact each code point in the given string

License

Apache License

Declaration

public static String randomlyRecaseCodePoints(Random random, String str) 

Method Source Code

//package com.java2s;
/**/*from   ww w .jav  a 2s.co  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Randomly upcases, downcases, or leaves intact each code point in the given string
     */
    public static String randomlyRecaseCodePoints(Random random, String str) {
        StringBuilder builder = new StringBuilder();
        int pos = 0;
        while (pos < str.length()) {
            int codePoint = str.codePointAt(pos);
            pos += Character.charCount(codePoint);
            switch (nextInt(random, 0, 2)) {
            case 0:
                builder.appendCodePoint(Character.toUpperCase(codePoint));
                break;
            case 1:
                builder.appendCodePoint(Character.toLowerCase(codePoint));
                break;
            case 2:
                builder.appendCodePoint(codePoint); // leave intact
            }
        }
        return builder.toString();
    }

    /** start and end are BOTH inclusive */
    public static int nextInt(Random r, int start, int end) {
        return start + r.nextInt(end - start + 1);
    }
}

Related

  1. randomFileString()
  2. randomFixedByteLengthUnicodeString(Random r, int length)
  3. randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
  4. randomize(String[] orderedList)
  5. randomizeMap(Map map)
  6. randomNumberString(int in)
  7. randomRealisticUnicodeString(Random r)
  8. randomString()
  9. randomString()