Groovy: Generate random string from given character set

后端 未结 6 488
温柔的废话
温柔的废话 2021-01-31 14:33

Using Groovy, I\'d like to generate a random sequence of characters from a given regular expression.

  • Allowed charaters are: [A-Z0-9]
  • Length o
6条回答
  •  一向
    一向 (楼主)
    2021-01-31 15:24

    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()}
    

提交回复
热议问题