Read a character at String in java?
I have a word
"unspecified"
, I want to search this word in string, ie.: "777 unspecifieduser 330"
I want to know this string have "unspecified" word or not?
in java, i'll appreciate the details
thanks in adva开发者_Go百科nce
Use the String.contains
method.
String str = "777 unspecifieduser 330";
if (str.contains("unspecified")) {
// ...
}
A slighly more general approach would be to do use regular expressions:
"777 unspecifieduser 330".matches(".*unspecified.*")
If you're interested in where the substring occurs. Use the String.indexOf
method.
boolean result = "777 unspecifieduser 330".contains("unspecified");
You don't have to use indexOf(), that's only useful if you want to do string manipulation. If you simply want to test for "existence", the contains method will suffice. You can use the contains method as follows:
if ("777 unspecifieduser 330".contains("unspecified")) {
//do something
}
String s = "777 unspecifieduser 330";
boolean contains = s.contains("unspecified");
精彩评论