How to search a word in a string?
For example
String text = \"Samsung Galaxy S Two\";
If I use text.contains(\"???\");
<
I know this is an old question, but I am writing here so that the next person who needs help can be helped.
You can use matches.
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
}
For most simple usage, you can use a StringTokenizer Look at this link. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
For using Regular expressions, Look at Patterns in android. http://developer.android.com/reference/java/util/regex/Pattern.html
Try this..
String string = "madam, i am Adam";
// Characters
// First occurrence of a c
int index = string.indexOf('a'); // 1
// Last occurrence
index = string.lastIndexOf('a'); // 14
// Not found
index = string.lastIndexOf('z'); // -1
// Substrings
// First occurrence
index = string.indexOf("dam"); // 2
// Last occurrence
index = string.lastIndexOf("dam"); // 13
// Not found
index = string.lastIndexOf("z"); // -1
use indexOf
:
int i= string.indexOf('1');
or substring
:
String s=string.substring("koko",0,1);
List<String> tokens = new ArrayList<String>();
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<tokens.size();i++)
{
if(tokens.get(i).contains(search))
{
System.out.println("Word is "+tokens.get(i));
break;//=====> Remove Break if you want to continue searching all the words which contains `axy`
}
}
output====>Galaxy