1

I want to generate unique 25 characters and numbers long string using the following code,

public static String generateString() {
   String uuid = UUID.randomUUID().toString();
   return uuid;
}

How I can set the size of the generated String and remove the - symbols?

Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

5

UUID is of fixed size, if you just want to get rid of - use replace as mentioned above or else you have to substring to get the desired length. Also, keep in mind UUID has defined length of 16-byte.

Reference: java.util.UUID.randomUUID().toString() length

Kunal
  • 371
  • 2
  • 7
3

Use replace()

uuid = uuid.replace("-", "");
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80