简体   繁体   中英

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 advance

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM