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

I'm using RandomStringGenerator from commons-text 1.2-SNAPSHOT to produce random user names and passwords for tests. Using those generated strings in Selenium with Chrome driver can cause

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)

Is there any way to produce BMP strings with existing functions already or do I have to go the obvious way to research all BMP codepoints to select randomly from them and risk to reinvent the wheel.

解决方案

RandomStringGenerator allows you to create a generator that is filtered by a specified range and a filtering function. Below, in Kotlin, is a function I use to generate a random BMP unicode string that's compatible with Selenium's chrome driver.

If you don't care if your random string includes non-printable characters, than you can remove the filteredBy.

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值