zoukankan      html  css  js  c++  java
  • 返回随机字符串

     1 public class Test01 {
     2     public static void main(String[] args) {
     3         String code = Test01.randomCode(32);
     4         System.out.println(code);
     5     }
     6 
     7     // 返回随机字符串,
     8     public static String randomCode(int length) {
     9         // 定义字符串的范围
    10         char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
    11                 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
    12                 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
    13                 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
    14                 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
    15                 'v', 'w', 'x', 'y', 'z' };
    16 
    17         // 随机函数
    18         Random random = new Random();
    19 
    20         // 用于获取到字符串后添加,
    21         StringBuilder builder = new StringBuilder();
    22 
    23         for (int i = 0; i < length; i++) {
    24             int b = random.nextInt(62);
    25             builder.append(codeSequence[b]);
    26         }
    27         return builder.toString();
    28     }
    29 }
  • 相关阅读:
    Git 修改已提交的commit注释
    设置git bash中显示行号等
    JS 获取字符串长度
    泛型接口
    约束
    泛型方法
    泛型
    重载运算符
    自定义转换
    装箱和拆箱
  • 原文地址:https://www.cnblogs.com/xjbBill/p/6121503.html
Copyright © 2011-2022 走看看