📜  在java代码示例中生成随机字符串

📅  最后修改于: 2022-03-11 14:52:48.193000             🧑  作者: Mango

代码示例2
public static String getRandomString(int size) {
      // The string that we will return
    String rand = "";
      // The chars that are used to generate the random string
    String chars = "1234567890-=!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?";
      // Loop based on the requested size
      for (int i = 0; i < size; i++) {
          // Add a random char from the chars string to the rand string
        rand += chars.toCharArray()[new Random().nextInt(chars.length())];
    }
      // Return the random string
    return rand;
}