有時我們會想判斷一個string是否包含一些特定的名詞 ,
通常要達到這樣的目的 , 最直覺的 , 我們可以用Pattern and Matcher ,
並用Matcher中的find來判斷是否這個String是否包含我們想要的一些名詞.
不過更簡單的 , 我們可以直接用String class中的indexOf來達這樣的目的.
try
{
BufferedReader in = new BufferedReader(new FileReader("testData"));
String str;
while((str = in.readLine())!=null)
{
System.out.println(str.indexOf("化妝水"));
}
}
catch(IOException e)
{
System.out.println(e);
}
Ther result will return -1 if the string does not contain "化妝水" ,
or it will return the position of the string .
全站熱搜