string Groovy:从给定的字符集生成随机字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8138164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Groovy: Generate random string from given character set
提问by Robert Strauch
Using Groovy, I'd like to generate a random sequence of characters from a given regular expression.
使用 Groovy,我想从给定的正则表达式生成随机字符序列。
- Allowed charaters are:
[A-Z0-9]
- Length of generated sequence: 9
- 允许的字符是:
[A-Z0-9]
- 生成序列的长度:9
Example: A586FT3HS
例子: A586FT3HS
However, I can't find any code snippet which would help me. If using regular expressions is too complicated, I'll be fine defining the allowed set of characters manually.
但是,我找不到任何对我有帮助的代码片段。如果使用正则表达式太复杂,我可以手动定义允许的字符集。
回答by tim_yates
If you don't want to use apache commons, or aren't using Grails, an alternative is:
如果您不想使用 apache commons,或者不使用 Grails,另一种方法是:
def generator = { String alphabet, int n ->
new Random().with {
(1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
}
}
generator( (('A'..'Z')+('0'..'9')).join(), 9 )
but again, you'll need to make your alphabet
yourself... I don't know of anything which can parse a regular expression and extract out an alphabet of passing characters...
但同样,你需要alphabet
自己做......我不知道有什么可以解析正则表达式并提取出传递字符的字母表......
回答by Dónal
import org.apache.commons.lang.RandomStringUtils
String charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = 9
String randomString = RandomStringUtils.random(length, charset.toCharArray())
The imported class RandomStringUtils
is already on the Grails classpath, so you shouldn't need to add anything to the classpath if you're writing a Grails app.
导入的类RandomStringUtils
已经在 Grails 类路径中,因此如果您正在编写 Grails 应用程序,则不需要向类路径添加任何内容。
Update
更新
If you only want alphanumeric characters to be included in the String you can replace the above with
如果您只想在字符串中包含字母数字字符,您可以将上面的替换为
String randomString = org.apache.commons.lang.RandomStringUtils.random(9, true, true)
回答by user3215161
For SoupUI users:
对于 SoupUI 用户:
def generator = { String alphabet, int n ->
new Random().with {
(1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
}
}
randomValue = generator( (('A'..'Z')+('0'..'9')+('a'..'z')).join(), 15 )
testRunner.getTestCase().setPropertyValue("randomNumber", randomValue);
回答by vkrams
Here is a single line command/statement to generate random text string
这是生成随机文本字符串的单行命令/语句
print new Random().with {(1..9).collect {(('a'..'z')).join()[ nextInt((('a'..'z')).join().length())]}.join()}
or
或者
def randText = print new Random().with {(1..9).collect {(('a'..'z')).join()[ nextInt((('a'..'z')).join().length())]}.join()}
回答by Sjoerd
回答by Abdelsalam Shahlol
This code is pure Groovy I found on the web:
这段代码是我在网上找到的纯 Groovy:
def key
def giveMeKey(){
String alphabet = (('A'..'N')+('P'..'Z')+('a'..'k')+('m'..'z')+('2'..'9')).join()
def length = 6
key = new Random().with {
(1..length).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
}
return key
}
The return string is good enough for local use:
返回字符串足以在本地使用:
BFx9PU
MkbRaE
FKvupt
gEwjby
Gk2kK6
uJmzLB
WRJGKL
RnSUQT