0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javaによる任意の文字列作成コード

Posted at
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();
    }
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?