Fat*_*Crg 6 string dart flutter
如何从长文件中获取前几个词(摘录)String。我有一个大故事类型字符串,需要在屏幕上显示前 5-10 个单词,其余的要在下一个屏幕上显示。那么有没有办法做到这一点。我搜索了很多,但无法解决问题。例如:获取我们使用的第一个字母
String sentence = "My single Sentence";
sentence[0] //M
Run Code Online (Sandbox Code Playgroud)
同样,我需要得到一些词。例如:
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...
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用子字符串方法String..
String myString = 'abcdefghijklmnopqrstuvwxyz';
String smallString = myString.substring(0,5); //<-- this string will be abcde
Run Code Online (Sandbox Code Playgroud)
对于特定用例,假设无论结果字符串中有多少单词,您都需要最多 30 个字符,那么您可以编写这样的函数。
String smallSentence(String bigSentence){
if(bigSentence.length > 30){
return bigSentence.substring(0,30) + '...';
}
else{
return bigSentence;
}
}
Run Code Online (Sandbox Code Playgroud)
如果要求专门获取前几个单词,无论结果字符串的长度如何,都可以说前 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) + '...';
}
Run Code Online (Sandbox Code Playgroud)
用于创建的附加编辑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) + '...';
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它
String bigText = 'very big text';
print(bigText.smallSentence());
print(bigText.firstFewWords());
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:另一种解决方案
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,
),
Run Code Online (Sandbox Code Playgroud)
要拆分字符串,我们需要执行以下几个步骤:
把它变成列表
制作新的小列表
将其转换回字符串
要将 String 变成 List,我们可以使用这个
String bigSentence = 'Lorem Ipsum is'
bigSentence.split(" ")
// ["Lorem", "Ipsum", "is"]
Run Code Online (Sandbox Code Playgroud)
制作新的较小的列表,例如获取前两个单词,
我们使用子列表
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.sublist(0, 2)
// ["Lorem", "Ipsum"]
Run Code Online (Sandbox Code Playgroud)
将其转换回字符串
List<String> smaller = ["Lorem", "Ipsum"]
smaller.join(" ")
// "Lorem Ipsum"
Run Code Online (Sandbox Code Playgroud)
最后,我们可以将其简化为 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 ...
}
Run Code Online (Sandbox Code Playgroud)
实际上,还有另一种选择可以按照步骤 2 中的建议实现新的较小列表
用take
List<String> splitted = ["Lorem", "Ipsum", "is"]
splitted.take(2)
// ["Lorem", "Ipsum"]
Run Code Online (Sandbox Code Playgroud)
正如建议scrimau,上面可能会遇到性能第一种方法击中其效率低下分裂也许千言万语首先,为了得到几个单词。
我刚刚了解到Dart有Runes,在这种情况下可能会对我们有所帮助。
要迭代 String,首先我们需要将其转换为 Runes。如此处所述,符文具有可迭代的
if (findCount < 1) {
return '';
}
Run Code Online (Sandbox Code Playgroud)
Runes spaceRunes = Runes(wordSeparator);
Runes sentenceRunes = Runes(sentence);
Run Code Online (Sandbox Code Playgroud)
String finalString = "";
Run Code Online (Sandbox Code Playgroud)
最重要的部分在这里,对于你的情况,我们需要找到 Space ' '
所以,稍后如果我们已经找到足够的空间,我们只需返回 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);
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)