[英]Issue in Combining splitted String
我從“ Web 2.0 Wikipedia”文章中提取了文本,並將其拆分為“句子”。 之后,我將創建“字符串”,每個字符串包含5個句子。
提取后,文本如下所示,在EditText
下面是我的代碼
finalText = textField.getText().toString();
String[] textArrayWithFullStop = finalText.split("\\. ");
String colelctionOfFiveSentences = "";
List<String>textCollection = new ArrayList<String>();
for(int i=0;i<textArrayWithFullStop.length;i++)
{
colelctionOfFiveSentences = colelctionOfFiveSentences + textArrayWithFullStop[i];
if( (i%5==0) )
{
textCollection.add(colelctionOfFiveSentences);
colelctionOfFiveSentences = "";
}
}
但是,當我使用Toast
顯示文本時,這給出了
Toast.makeText(Talk.this, textCollection.get(0), Toast.LENGTH_LONG).show();
如您所見,這只是一句話! 但我希望它能有5個句子!
另一件事是,第二句話從其他地方開始。 在這里,我如何將其提取到Toast
Toast.makeText(Talk.this, textCollection.get(1), Toast.LENGTH_LONG).show();
這對我來說毫無意義! 如何將文本正確地分成句子,並創建每個包含5個句子的Strings
?
在textArrayWithFullStop[i]
添加". "
colelctionOfFiveSentences = colelctionOfFiveSentences + textArrayWithFullStop[i]+". ";
問題在於,對於第一句話,0%5 = 0,因此將其立即添加到數組列表中。 您應該使用其他計數器而不是mod。
finalText = textField.getText().toString();
String[] textArrayWithFullStop = finalText.split("\\. ");
String colelctionOfFiveSentences = "";
int sentenceAdded = 0;
List<String>textCollection = new ArrayList<String>();
for(int i=0;i<textArrayWithFullStop.length;i++)
{
colelctionOfFiveSentences += textArrayWithFullStop[i] + ". ";
sentenceAdded++;
if(sentenceAdded == 5)
{
textCollection.add(colelctionOfFiveSentences);
colelctionOfFiveSentences = "";
sentenceAdded = 0;
}
}
我相信,如果您將mod行修改為:
if(i%5==4)
您將擁有所需的東西。
您可能意識到了這一點,但是還有其他原因導致某人可能使用“。”,而實際上並沒有結束一個句子。
I spoke to John and he said... "I went to the store.
Then I went to the Tennis courts.",
and I don't believe he was telling the truth because
1. Why would someone go to play tennis after going to the store and
2. John has no legs!
I had to ask, am I going to let him get away with these lies?
這是兩個不以句號結尾的句子,它們會誤導您的代碼,使您認為這5個句子在完全錯誤的地方被分解,因此這種方法確實充滿了問題。 但是,作為一種拆分字符串的練習,我想它與其他方法一樣好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.