简体   繁体   English

在 java 的字符串中读取一个字符?

[英]Read a character at String in java?

I have a word我有话说

"unspecified" “未指定”

, I want to search this word in string, ie.: "777 unspecifieduser 330" ,我想在字符串中搜索这个词,即: "777 unspecifieduser 330"

I want to know this string have "unspecified" word or not?我想知道这个字符串有没有“未指定”的词?

in java, i'll appreciate the details在 java 中,我会欣赏细节

thanks in advance提前致谢

Use the String.contains method.使用String.contains方法。

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.如果您对 substring 出现的位置感兴趣。 Use the String.indexOf method.使用String.indexOf方法。

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.您不必使用 indexOf(),这仅在您想要进行字符串操作时才有用。 If you simply want to test for "existence", the contains method will suffice.如果您只是想测试“存在”,则 contains 方法就足够了。 You can use the contains method as follows:您可以按如下方式使用 contains 方法:

if ("777 unspecifieduser 330".contains("unspecified")) {
    //do something
}
String s = "777 unspecifieduser 330";
boolean contains = s.contains("unspecified");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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