首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中产生随机的基本多语言平面(BMP)字符串?

问如何在Java中产生随机的基本多语言平面(BMP)字符串?
EN

Stack Overflow用户
提问于 2017-10-26 17:39:33
回答 1查看 324关注 0票数 2

我使用commons-text 1.2-SNAPSHOT中的RandomStringGenerator生成随机用户名和密码用于测试。在Selenium和Chrome驱动程序中使用这些生成的字符串可能会导致

代码语言:javascript
复制
org.openqa.selenium.WebDriverException: 
unknown error: ChromeDriver only supports characters in the BMP
  (Session info: chrome=62.0.3202.62)
  (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.13.0-16-generic x86_64) (WARNING: The server did not provide any stacktrace information)

有没有办法用现有的函数生成BMP字符串,或者我必须用显而易见的方法来研究所有的BMP代码点,从它们中随机选择,冒着重新发明轮子的风险。

不是How to handle "org.openqa.selenium.WebDriverException: ChromeDriver only supports characters in the BMP" exception?的副本,因为我对用于其他目的的解决方案也很感兴趣。

EN

回答 1

Stack Overflow用户

发布于 2020-07-25 05:10:13

RandomStringGenerator允许您创建一个生成器,该生成器根据指定的范围和过滤函数进行过滤。下面是我用Kotlin编写的一个函数,用于生成与Selenium的chrome驱动程序兼容的随机BMP unicode字符串。

如果您不关心您的随机字符串是否包含不可打印的字符,那么您可以删除filteredBy。

代码语言:javascript
复制
private val randomBMPStringGenerator: RandomStringGenerator = RandomStringGenerator.Builder().withinRange(0x0000, 0xFFFF).filteredBy(CharacterPredicate {
    c ->
    val block: UnicodeBlock = UnicodeBlock.of(c)
    !Character.isISOControl(c) && block !== UnicodeBlock.SPECIALS
}).build()

randomBMPStringGenerator.generate(wordLength)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46950897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档