<分区>
我想从一个给定的字符串中创建两个随机子字符串,加入后会得到我以前的字符串。怎么做到的?
例子:
"My name is Robert"是完整的字符串,那么子字符串可以像
sub1:"我的名字"
sub2:"是罗伯特"
<分区>
我想从一个给定的字符串中创建两个随机子字符串,加入后会得到我以前的字符串。怎么做到的?
例子:
"My name is Robert"是完整的字符串,那么子字符串可以像
sub1:"我的名字"
sub2:"是罗伯特"
最佳答案
以下将分配一个以字符串长度为界的随机索引,然后生成两个在随机索引处拆分的子字符串。
Random rand = new Random(); // initialize Random
int index = rand.nextInt(str.length()); // get random integer less than string length
String sub0 = str.substring(0, index); // get substring from 0 to the random index value
String sub1 = str.substring(index); // get substring from random index value to end
关于java - 在空间 (""处随机拆分一个字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40621833/