How to generate a random alpha-numeric string

前端 未结 30 2566
忘掉有多难
忘掉有多难 2020-11-21 05:38

I\'ve been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifie

30条回答
  •  梦谈多话
    2020-11-21 06:06

    Java supplies a way of doing this directly. If you don't want the dashes, they are easy to strip out. Just use uuid.replace("-", "")

    import java.util.UUID;
    
    public class randomStringGenerator {
        public static void main(String[] args) {
            System.out.println(generateString());
        }
    
        public static String generateString() {
            String uuid = UUID.randomUUID().toString();
            return "uuid = " + uuid;
        }
    }
    

    Output

    uuid = 2d7428a6-b58c-4008-8575-f05549f16316
    

提交回复
热议问题