如何从长String中获取头几个单词(摘录)。我有一个大故事类型字符串,需要在屏幕上显示前5-10个单词,并保留在下一个屏幕上显示。那么有什么办法可以做到。我找了很多东西,但没能解决这个问题。为了得到我们使用的第一个字母
String sentence = "My single Sentence";
sentence[0] //M同样的,我也需要说几句话。例:
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...发布于 2020-07-21 08:18:40
您可以像这样在子串上使用String方法。
String myString = 'abcdefghijklmnopqrstuvwxyz';
String smallString = myString.substring(0,5); //<-- this string will be abcde对于一个特定的用例,如果您想要最多30个字符,不管结果字符串中的单词数如何,那么您可以编写这样一个函数。
String smallSentence(String bigSentence){
if(bigSentence.length > 30){
return bigSentence.substring(0,30) + '...';
}
else{
return bigSentence;
}
}如果要求是专门获取前几个单词,让我们说前6个单词,不管结果字符串的长度,那么您可以编写如下所示的函数。我们还需要在indexOf上使用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) + '...';
}创建extension -的附加编辑
您可以在extension上创建一个String,如下所示
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) + '...';
}
}像这样使用它
String bigText = 'very big text';
print(bigText.smallSentence());
print(bigText.firstFewWords());发布于 2020-07-21 08:45:49
A.简单的方法
要拆分字符串,我们需要有几个步骤:
第一步
要将字符串转换为列表,我们可以使用以下方法
String bigSentence = 'Lorem Ipsum is'
bigSentence.split(" ")
// ["Lorem", "Ipsum", "is"]第二步
做新的更小的列表,例如得到前两个单词,
我们使用子表
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.sublist(0, 2)
// ["Lorem", "Ipsum"]第三步
将其转换回字符串
List<String> smaller = ["Lorem", "Ipsum"]
smaller.join(" ")
// "Lorem Ipsum"全功能代码
最后,我们可以将其简化为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 ...
}备选方案
实际上,还有另一种方法可以实现新的更小的列表,如步骤2所建议的那样
使用take
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.take(2)
// ["Lorem", "Ipsum"]B.艰难的道路
正如scrimau所建议的那样,上面的第一种方法可能会经历性能命中的,因为它的低效率首先分裂了数千个单词,以便得到几个单词。
我刚刚了解到Dart有符文,在这种情况下这可能会对我们有所帮助。
要迭代字符串,首先我们需要将其转换为Runes。如前所述,Runes 有可迭代
我们需要有几个步骤:
1.验证查找计数
if (findCount < 1) {
return '';
}2.将分隔符和句子转换为符文
Runes spaceRunes = Runes(wordSeparator);
Runes sentenceRunes = Runes(sentence);3.准备最后的弦
String finalString = "";4.迭代符文
最重要的部分在这里,,对于你的情况,我们需要找到空间‘’
所以,稍后如果我们已经找到足够的空间,我们只需返回Final String
如果我们没有找到足够的空间,那么迭代更多,然后追加Final String
请注意,这里我们使用.single,因此单词分隔符必须仅为单个字符。
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
}发布于 2020-07-21 08:26:20
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);
}编辑:另一个解决方案
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,
),https://stackoverflow.com/questions/63010255
复制相似问题