Android 从字符串中删除停止字
我需要从字符串中删除停止字。我使用以下代码删除停止字并在textView中设置最终输出。但是当我运行代码时,它总是给输出“bug”。换句话说,它总是给我最后一个字符串作为输出。请检查我的代码和帮助Android 从字符串中删除停止字,android,string,stop-words,removeall,Android,String,Stop Words,Removeall,我需要从字符串中删除停止字。我使用以下代码删除停止字并在textView中设置最终输出。但是当我运行代码时,它总是给输出“bug”。换句话说,它总是给我最后一个字符串作为输出。请检查我的代码和帮助 public class Testing extends Activity { TextView t1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method
public class Testing extends Activity {
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
t1= (TextView)findViewById(R.id.textView1);
String s="I love this phone, its super fast and there's so" +
" much new and cool things with jelly bean....but of recently I've seen some bugs.";
String[] words = s.split(" ");
ArrayList<String> wordsList = new ArrayList<String>();
Set<String> stopWordsSet = new HashSet<String>();
stopWordsSet.add("I");
stopWordsSet.add("THIS");
stopWordsSet.add("AND");
stopWordsSet.add("THERE'S");
for(String word : words)
{
String wordCompare = word.toUpperCase();
if(!stopWordsSet.contains(wordCompare))
{
wordsList.add(word);
}
}
for (String str : wordsList){
System.out.print(str+" ");
t1.setText(str);
}
}
公共类测试扩展了活动{
文本视图t1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
t1=(TextView)findViewById(R.id.textView1);
String s=“我喜欢这款手机,它的速度非常快,而且有很多”+
“果冻豆有很多新的很酷的东西……但最近我看到了一些虫子。”;
字符串[]字=s.split(“”);
ArrayList wordsList=新的ArrayList();
Set stopWordsSet=new HashSet();
stopWordsSet.添加(“I”);
stopWordsSet.添加(“本”);
stopWordsSet.添加(“和”);
stopWordsSet.add(“有”);
for(字符串字:字)
{
字符串wordCompare=word.toUpperCase();
如果(!stopWordsSet.contains(wordCompare))
{
wordsList.add(word);
}
}
for(字符串str:wordsList){
系统输出打印(str+“”);
t1.setText(str);
}
}
t1.setText(str);
表示它不关心前面的文本是什么。它将最后一个文本放入循环中。因此请改用
或者将每个str
附加到单个字符串中,并在循环后的TextView
中设置该字符串。输出为“bugs”。由于这行代码:
t1.setText(str);
每次在循环中都会重新写入textview。因为上一次迭代的单词是“bugs”,textview会显示bugs
如果要追加字符串而不是重新写入,请使用:
t1.append(str);
希望有帮助。@ArslanAli很高兴听到这个消息。你能告诉我如何在最终输出中留出空格吗?我得到了我想要的输出,但所有的单词都被连接起来了。它们应该用空格分隔spaces@ArslanAli:D你在哪里?哦,我很抱歉我对这个答案投了1票,我认为这是接受,我忘了勾选:(
t1.append(str);