在Android的字符串/ Java的取词 [英] fetch a word in strings Android/Java
本文介绍了在Android的字符串/ Java的取词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我与Android / Java编程开始。我想尝试找出句子中的一个词。说,在EditText上用户输入一个句子,即我的朋友是个牛仔。该应用程序应该能够检测到字牛仔从字符串数组。即
So I'm beginning with Android/Java programming. I want to try to find a word in a sentence. Say the user inputs in the EditText a sentence i.e. "My friend is a cowboy." The app should detect the word cowboy from a the String array. i.e.
String[] words = {"cowboy", "animal", "monster"};
这是我已经试过:
String[] words = {"cowboy", "animal", "monster"};
Boolean b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById (R.id.editText1);
view = (TextView) findViewById (R.id.textView1);
ok = (Button) findViewById (R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String string = text.getText().toString();
b = string.indexOf("cowboy") > 0;
view.setText(b.toString());
}
});
}
但它给我一个虚假的答案。有什么不对我的code?而在IndexOf方法,我不知道如何把字符串数组为。我希望有人能帮助我解决这个问题。谢谢你。
But it's giving me a false answer. What is wrong with my code? And in the indexOf method, I don't know how to put the string array for that. I hope someone will help me fix this problem. Thanks.
推荐答案
请尝试以下操作:
String[] words = {"cowboy", "animal", "monster"};
String s = "My friend is a Cowboy";
boolean check = false;
for (int i = 0; i < words.length; i++) {
if (s.toLowerCase().contains(words[i].toLowerCase())) {
check = true;
} else {
}
}
if (check) {
System.out.println("Yes");
} else {
System.out.println("No");
}
这篇关于在Android的字符串/ Java的取词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文