Pages

Check word in String

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.

**************************************************************
COMPLETE SOURCE CODE FOR : CheckWordInString.java
**************************************************************


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


**************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You