Reputation: 4460
How can I generate a String of a given size?
int someLimit = GlobalLimits.BULK_SIZE;
I want 3 Strings which satisfy the below conditions.
This is what I have tried so far.
private String getLowerRandomString(int upto){
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < upto; i++){
sBuilder.append("A");
}
return sBuilder.toString();
}
The problem what I see is, if my limit = 10000, it still loop up-to 9999 which is unnecessary. Share if you know a better approach than this. Thank you.
FYI: I was writing a unit test for a simple helper method.
public boolean isBulk(String text){
int bulkLimit = ImportToolkit.getSizeLimit();
if (text != null && text.length() > bulkLimit){
return true;
}
return false;
}
So, I want to pass different sizes of strings as parameters to this method and want to assert whether it gives me expected results.
Upvotes: 4
Views: 8140
Reputation: 5436
If you're only concerned about the string's length, you can just do this:
String str = new String(new char[SIZE]);
This is useful, for example, when you want to test if a given method fails when it is given a string of a specific length.
Upvotes: 2
Reputation: 39226
Please refer the link which has the secure way to create random string for any specified length. I have used it in my project and it worked fine.
Upvotes: 0
Reputation: 491
Take a look at Xeger library here . Your code would go like this.
public static void main(String[] args){
String regex="([0-9]{100})";
System.out.println(new Xeger(regex).generate());
}
Output :- 5262521775458685393102235626265923114201782357574146361524512101753254114567366125627554628148696482
You can change 100 or 1000 according to your need.
Jar Location here.
-Sid
Upvotes: 1
Reputation: 5575
What about using apache commons? It has a RandomStringUtils class that provides exactly the functionality you're looking for, but in the end it loops too...
org.apache.commons.lang3.RandomStringUtils#randomAlphanumeric(int count)
From JavaDoc
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of alpha-numeric characters.
Parameters:
count - the length of random string to create
Returns:
the random string
If it doesn't need to be random there is another, cheaper method in Stringutils:
org.apache.commons.lang3.StringUtils#repeat(char, int)
But in the end it too loops...
From JavaDoc
Returns padding using the specified delimiter repeated to a given length.
StringUtils.repeat('e', 0) = ""
StringUtils.repeat('e', 3) = "eee"
StringUtils.repeat('e', -2) = ""
Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using repeat(String, int) instead.
Parameters:
ch - character to repeat
repeat - number of times to repeat char, negative treated as zero
Returns:
String with repeated character
See Also:
repeat(String, int)
Upvotes: 4
Reputation: 984
Use Random to generate a number less than Limit and greater than limit and pass to function to generate a string of that length
lowerLimit= 0 + (int)(Math.random() * maximum);
higherLimit= minimum + (int)(Math.random() * maximum);
smallerString= getLowerRandomString(lowerLimit);
greaterString= getLowerRandomString(higherLimit);
For help on limiting random number, please check this post.How do I generate random integers within a specific range in Java?
Upvotes: 0
Reputation: 22
If you only concern about the string length, Do a mathematical operation that result a big value. Convert that result value as string and perform your validation. If you do not want numeric then, instead of appending single string 'a' to stringBuilder. Append a big string and in your for loop increment based on the length of the string that you append to string builder.
Upvotes: 0