gpt4 book ai didi

java - 如何将两个字符串与随机位置android结合起来

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:59 26 4
gpt4 key购买 nike

我正在尝试生成一个许可证 key ,其中两个字符串组合成另一个字符串。

String key1 = "X1X2X3X4..Xn"; //this is for the imei key cellphone
String key2 = "Y1Y2Y3Y4M1M2D1D2H1H2m1m2"; //this is a key to mix with the first one

组合的结果应该是这样的:

String result = "D1H1X1X2X3Y2Y4X4X5...XnY3Y1D2m2m1H1H2";

我像这样每两个空格拆分我的字符串,然后保存到一个数组中:

String [] key1splited = splitStringEvery(key1, 2);
String [] key2splited = splitStringEvery(key2, 2);

public String[] splitStringEvery(String s, int interval) {
int arrayLength = (int) Math.ceil(((s.length() / (double)interval);
String[] result = new String[arrayLength];

int j = 0;
int lastIndex = result.length - 1;
for (int i = 0; i < lastIndex; i++) {
result[i] = s.substring(j, j + interval);
j += interval;
}
result[lastIndex] = s.substring(j);

return result;
}

我怎样才能使我的字符串组合得到如下所示的结果:

String result = "D1H1X1X2X3Y2Y4X4X5...XnY3Y1D2m2m1H1H2";

我希望有人能告诉我如何解决这个问题。

我正在尝试做这样的事情,但这是非常糟糕的方法:

static String res = "";
String[] key1splited = splitStringEvery(key1, 2);
String[] key2splited = splitStringEvery(key2, 2);
for (int i = 0; i < key2splited.length; i++) {
if (key2splited[i].equals("D1")) {
res = key2splited[i];
}
if (key2splited[i].equals("H1")) {
res += key2splited[i];
for (int j = 0; j < key1splited.length; j++) {
if (key1splited[j].equals("X1")) {
res += key1splited[j];
}
if (key1splited[j].equals("X2")) {
res += key1splited[j];
}
if (key1splited[j].equals("X3")) {
res += key1splited[j];
}
}
}
}

等等,但这不是一个好的方法,因为字符串会发生变化。

最佳答案

我认为最简单的方法是将 token 从每个键中分离出来(2 个字符的长 token ,如 X1、D1 等),然后将这些 token 组合成一个 String .

此外,您打乱 String,提取随机标记并构建许可证 key :

Random rand = new Random(); // generate Random object only once for efficiency purposes

// ... rest of your code

String getLicenceKey(String key1, String key2){
List<String> tokens = new ArrayList<String>();

// add tokens from key1
for(int i = 0; i < key1.length(); i += 2) {
tokens.add(key1.substring(i, i + 2));
}

// add tokens from key2
for(int i = 0; i < key2.length(); i += 2) {
tokens.add(key2.substring(i, i + 2));
}

// build the random result out of the tokens
StringBuilder result = new StringBuilder();
while(tokens.size() != 0){
int randomPos = rand.nextInt(tokens.size());
result.append(tokens.remove(randomPos));
}

return result.toString();
}

您提供的输入的示例输出:

m2XnD2m1H2X1..Y3Y1Y2Y4M1X2M2D1H1X4X3
H2Y2X4M1M2H1Y3Y1m2X1X2D1m1Xn..X3Y4D2
X1X4X3H2D2H1..M2m2Y3m1Y4M1D1Y1X2XnY2

key1="A1B1C1", key2="D1E1F1"的示例输出:

D1F1B1A1C1E1
C1A1D1B1E1F1
F1E1B1C1D1A1

关于java - 如何将两个字符串与随机位置android结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com