gpt4 book ai didi

flutter - Flutter/Dart:将字符串拆分为所有可能的组合

转载 作者:行者123 更新时间:2023-12-03 04:48:34 25 4
gpt4 key购买 nike

如何将字符串分成所有可用组合的大块?例如:

“12345”

将输出:

[1,
12
123,
1234,
12345,
2,
23
234,
2345,
3,
34,
345,
4,
45]

据我所知:

String title = "12345";
List<String> keywordsList = List();
String temp = "";
String temp2 = "";
for (int i = 0; i < title.length; i++) {
temp = temp + title[i];
if (temp.length > 1) temp2 = temp2 + title[i];
keywordsList.add(temp);
if (temp2.length != 0) keywordsList.add(temp2);
}
print(keywordsList);
return keywordsList;

},

结果是:

[1,12,2,123,23,1234,234,12345,2345]

super 卡住了,将不胜感激。

提前致谢!

最佳答案

您可以通过以下方式实现。

String number = '12345';
List<String> listnumber = number.split("");
List<int> output = [];
for (int i = 0; i < listnumber.length; i++) {
if (i != listnumber.length - 1) {
output.add(int.parse(listnumber[i]));
}
List<String> temp = [listnumber[i]];
for (int j = i + 1; j < listnumber.length; j++) {
temp.add(listnumber[j]);
output.add(int.parse(temp.join()));
}
}
print(output.toString());

关于flutter - Flutter/Dart:将字符串拆分为所有可能的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61817580/

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