Sunday, April 25, 2010

Check word in String  

0 comments

Complete source code below will show you how to check word if exist or not in a String. It will compare a word that we want to check with all word that separate by white space in targeted String. Comparison will ignore case.

view plainprint?
import java.util.StringTokenizer;

public class CheckWordInString
{
public static void main(String[]args)
{
String a="Hello World !";

/*
*Word(hello) that we want
*to check if exist
*or not in String a
*/
String b="hello";

/*
*Get all word that
*separate by white space
*in String a
*/
StringTokenizer st
=new StringTokenizer(a);

while(st.hasMoreTokens())
{
String temp=st.nextToken();

/*
*Compare with
*ignore case
*/
if(temp.equalsIgnoreCase(b))
{
System.out.println
("b exist in a");
break;
}
}
}
}

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



0 comments: to “ Check word in String

Extension Factory Builder