How to get first few words (excerpts) from a long String
. I have a big story type String and need to display the first 5-10 words on the screen and remaining to be displayed on the next screen. So is there any way to get so. I searched a lot but couldn't resolve the issue.
Eg: TO get the first letter we use
String sentence = "My single Sentence";
sentence[0] //M
In the same way, I need to get some words. Eg:
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
''';
//code to get the excerpt()
//predicted output=> Lorem Ipsum is simply dummy text...
To find the substring of a string in Dart, call substring() method on the String and pass the starting position and ending position of the substring in this string, as arguments.
You can use substring method on String
like this..
String myString = 'abcdefghijklmnopqrstuvwxyz';
String smallString = myString.substring(0,5); //<-- this string will be abcde
For a specific use case, lets say if you want maximum of 30 characters no matter the number of words in the resulting string, then you could write a function like this..
String smallSentence(String bigSentence){
if(bigSentence.length > 30){
return bigSentence.substring(0,30) + '...';
}
else{
return bigSentence;
}
}
If the requirement is to specifically get the first few words, lets say first 6 words no matter the resulting string's length, then you could write a function like below. We will also need to use indexOf method on String
.
String firstFewWords(String bigSentence){
int startIndex = 0, indexOfSpace;
for(int i = 0; i < 6; i++){
indexOfSpace = bigSentence.indexOf(' ', startIndex);
if(indexOfSpace == -1){ //-1 is when character is not found
return bigSentence;
}
startIndex = indexOfSpace + 1;
}
return bigSentence.substring(0, indexOfSpace) + '...';
}
Additional Edit for creating extension
-
You can create an extension
on String
like so
extension PowerString on String {
String smallSentence() {
if (this.length > 30) {
return this.substring(0, 30) + '...';
} else {
return this;
}
}
String firstFewWords() {
int startIndex = 0, indexOfSpace;
for (int i = 0; i < 6; i++) {
indexOfSpace = this.indexOf(' ', startIndex);
if (indexOfSpace == -1) {
//-1 is when character is not found
return this;
}
startIndex = indexOfSpace + 1;
}
return this.substring(0, indexOfSpace) + '...';
}
}
And use it like this
String bigText = 'very big text';
print(bigText.smallSentence());
print(bigText.firstFewWords());
To Split String, we are required to have several steps :
Turn it to List
Make New Smaller List
Transform it back to String
To turn String into List, we can use this
String bigSentence = 'Lorem Ipsum is'
bigSentence.split(" ")
// ["Lorem", "Ipsum", "is"]
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"]
Transform it back to String
List<String> smaller = ["Lorem", "Ipsum"]
smaller.join(" ")
// "Lorem Ipsum"
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 ...
}
Actually, there is another options to achive New Smaller List as suggested in Step 2
Usetake
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.take(2)
// ["Lorem", "Ipsum"]
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
if (findCount < 1) {
return '';
}
Runes spaceRunes = Runes(wordSeparator);
Runes sentenceRunes = Runes(sentence);
String finalString = "";
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);
}
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
}
void main() {
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";
/// if length of string is greater than 6 words then append dots
bool appendDots = false;
/// this will split string into words
List<String> tempList = bigSentence.split(" ");
int start = 0;
int end = tempList.length;
/// extract first 6 words
if (end > 6) {
end = 6;
appendDots = true;
}
/// sublist of tempList
final selectedWords = tempList.sublist(start, end);
/// join the list with space
String output = selectedWords.join(" ");
if(appendDots){
output += "....";
}
print(output);
}
Edit : Another Solution
Text('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',
maxLines : 1,
overflow: TextOverflow.ellipsis,
),
String sentence = "My single Sentence";
print(sentence.split(" "));
Try using split(" ")
and assign it to a variable and then you can get the first word.
Had the same task, made an extension that truncate
whole words with required length and add to the end the suffix.
/// turuncate the [String] without cutting words. The length is calculated with the suffix.
extension Truncate on String {
String truncate({required int max, String suffix = ''}) {
return length < max
? this
: '${substring(0, substring(0, max - suffix.length).lastIndexOf(" "))}$suffix';
}
}
An example how to use
print('hello world two times!'.truncate(max: 15, suffix: '...'));
the result is hello world...
String sampleText = "Lorem ipsum";
sampleText.split(" ").elementAt(0) // Lorem
sampleText.split(" ").elementAt(1) // ipsum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With