如何在android中逐字搜索
如何在字符串中搜索单词?
例如
String text = "Samsung Galaxy S Two";
如果我使用text.contains("???");
它会得到任何相关的字母表,即使它不是一个合适的词,如“银河”中的“axy”。
有什么建议或解决方案吗?
List tokens = new ArrayList (); String text = "Samsung Galaxy S Two"; StringTokenizer st = new StringTokenizer(text); //("---- Split by space ------"); while (st.hasMoreElements()) { tokens.add(st.nextElement().toString()); } String search = "axy"; for(int i=0;i Remove Break if you want to continue searching all the words which contains `axy` } } output====>Galaxy
对于大多数简单用法,您可以使用StringTokenizer查看此链接。 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
要使用正则表达式,请查看android中的模式。 http://developer.android.com/reference/java/util/regex/Pattern.html
使用indexOf
:
int i= string.indexOf('1');
或substring
:
String s=string.substring("koko",0,1);
试试这个..
String string = "madam, i am Adam";
//人物
// First occurrence of ac int index = string.indexOf('a'); // 1 // Last occurrence index = string.lastIndexOf('a'); // 14 // Not found index = string.lastIndexOf('z'); // -1
//子字符串
// First occurrence index = string.indexOf("dam"); // 2 // Last occurrence index = string.lastIndexOf("dam"); // 13 // Not found index = string.lastIndexOf("z"); // -1
我知道这是一个老问题,但我写在这里是为了帮助下一个需要帮助的人。
你可以使用比赛 。
String str = "Hello, this is a trial text"; str1 = str.toLowerCase(); if(str1.matches(".*trial.*")) //this will search for the word "trial" in str1 { //Your Code }