A. Easy Way
To Split String, we are required to have several steps :
Turn it to List
Make New Smaller List
Transform it back to String
Step 1
To turn String into List, we can use this
String bigSentence = 'Lorem Ipsum is'
bigSentence.split(" ")
// ["Lorem", "Ipsum", "is"]
Step 2
Make New Smaller List, for example get first two words,
we use sublist
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.sublist(0, 2)
// ["Lorem", "Ipsum"]
Step 3
Transform it back to String
List<String> smaller = ["Lorem", "Ipsum"]
smaller.join(" ")
// "Lorem Ipsum"
Full Functional Code
at the end, we can simplify it to single line of code
String getFirstWords(String sentence, int wordCounts) {
return sentence.split(" ").sublist(0, wordCounts).join(" ");
}
String bigSentence = '''
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book
''';
main() {
String result = getFirstWords(bigSentence, 2);
print(result); // Lorem Ipsum
String resultDots = getFirstWords(bigSentence, 2) + " ...";
print(resultDots); // Lorem Ipsum ...
}
Alternatives
Actually, there is another options to achive New Smaller List as suggested in Step 2
Use take
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.take(2)
// ["Lorem", "Ipsum"]
B. Hard Way
As suggested by scrimau, the first method above may experience performance hit by its inefficiency splitting maybe thousands of words at first, in order to get several words.
I just learned that Dart has Runes, that may helps us in this case.
To iterate String, firstly we need to transform it into Runes. As stated here, Runes has iterable
We need to have several steps :
1. Validate find Count
if (findCount < 1) {
return '';
}
2. Turn Separator and Sentence into Runes
Runes spaceRunes = Runes(wordSeparator);
Runes sentenceRunes = Runes(sentence);
3. Prepare Final String
String finalString = "";
4. Iterate Runes
The most important part is here, for your case, we need to find Space ' '
So, later if we already found enough space, we just return the Final String
If we have not found enough space, iterate more and append then Final String
Also note here, we use .single, so the word separator must be single character only.
for (int letter in sentenceRunes) {
// <------ SPACE Character IS FOUND----->
if (letter == spaceRunes.single) {
findCount -= 1;
if (findCount < 1) {
return finalString;
}
}
// <------ NON-SPACE Character IS FOUND ----->
finalString += String.fromCharCode(letter);
}
Full Functional Code
String bigSentence = '''
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book
''';
String getFirstWordsFast(String sentence, String wordSeparator, int findCount) {
if (findCount < 1) {
return '';
}
Runes spaceRunes = Runes(wordSeparator);
Runes sentenceRunes = Runes(sentence);
String finalString = "";
for (int letter in sentenceRunes) {
if (letter == spaceRunes.single) {
findCount -= 1;
if (findCount < 1) {
return finalString;
}
}
finalString += String.fromCharCode(letter);
}
return finalString;
}
main() {
String shorterString = getFirstWordsFast(bigSentence, " ", 5);
print(shorterString); // Lorem Ipsum is simply dummy
}