Arbitrary string creation code by Java

import java.util.Random;

public class Test {
    public static void main(final String[] args) {
        final String romaji = "abcdefghijklmnopqrstuvwxyz";
        final String number = "0123456789";
        final String generator = romaji + romaji.toUpperCase() +number;
        System.out.println(getStr(generator, 10));
        System.out.println(getStr(generator, 10));
        System.out.println(getStr(generator, 10));
    }

    private static String getStr(final String input, final int length) {
        final Random random = new Random();
        final int num = input.length();
        final StringBuilder sb = new StringBuilder();
        final char[] chars = input.toCharArray();
        for (int i = 0; i < 10; i++) {
            sb.append(chars[random.nextInt(num)]);
        }

        return sb.toString();
    }
}

Recommended Posts

Arbitrary string creation code by Java
Java string
[Java] Divide a character string by a specified character
All same hash code string in Java
Script Java code
Java code TIPS
[Java] String padding
Java sample code 04
Java string processing
java file creation
Java sample code 01
Java character code
Split string (Java)
java directory creation
Code to escape a JSON string in Java
Eclipse ~ Java project creation ~
[Java] String comparison and && and ||
Java string multiple replacement
[Note] Java: String search
[Note] Java: String survey
About Java String class
[Java] How to cut out a character string character by character
Sample code for log output by Java + SLF4J + Logback
Java Discord Bot creation
[Java] Calculator program creation 1
Play non-XML valid characters in Java by specifying code values
Understand the Singleton pattern by comparing Java and JavaScript code
Understand the Iterator pattern by comparing Java and JavaScript code
Digital signature sample code (JAVA)
Java inflexible String class substring
Java parallelization code sample collection
Reflection on Java string manipulation
[Java] Creation of original annotation
Java test code method collection
Object-oriented summary by beginners (Java)
[Java] About String and StringBuilder
[Windows] Java code is garbled
Java in Visual Studio Code
Write Java8-like code in Java8
The story of not knowing the behavior of String by passing Java by reference
How to manage Java code automatically generated by jOOQ & Flyway sample
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)