0

How could I split a string into chunks of all available combinations? For example:

"12345"

Would output:

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

This is as far as I've gotten:

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;

},

Which results in:

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

Super stuck now, will appreciate any help.

Thanks in advance!

1
  • i don't think you are stuck on language syntax, you need a logical implemantation which is provided here. don't really know if this counts as a duplicate question. Commented May 15, 2020 at 11:19

1 Answer 1

1

You can achieve in following way.

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.