2 u010321179 u010321179 于 2013.08.26 13:55 提问

android/java中抓取一个词

在句子中找出一个词,比如用户输入了 "My friend is a cowboy"。应用就能根据数组检测出 cowboy。

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());
        }
    });

}

但是给出的结果不对。

2个回答

loFataMer
loFataMer   2013.08.26 15:25
已采纳
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");
}
Jave_
Jave_   2013.08.26 14:25

试了一下,你的代码并无错误

Csdn user default icon
上传中...
上传图片
插入图片