jeudi 10 mars 2022

How to generate sequential alphanumeric string in Java starting with specific string

I want to be able to generate sequential alphanumeric strings of a given length 8. Also, I want the string to start with a specific string let's say "ABC00". Just imagine a license plate starts with a specific string and other generated alphanumeric strings. I have tried a number of things I have seen here and not getting the desired results.

This is what I currently have

import java.security.SecureRandom;
import java.util.Random;

public class RandomString {


    static final String AB = "SCV00" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    static SecureRandom rnd = new SecureRandom();

    String randomString(int len){
        StringBuilder sb = new StringBuilder(len);
        for(int i = 0; i < len; i++)
            sb.append(AB.charAt(rnd.nextInt(AB.length())));
        return sb.toString();
    }

}

I want my output to look like this. When users provide the number of IDs they want I should be able to generate it. Say, user wants 3 IDs. I should have

SCV00Auh89 SCV00BZh98 SCV98uhsh7




Aucun commentaire:

Enregistrer un commentaire