有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

字符串在java中搜索单词?

我有一个String样的

 String test = "voiceemailchat";
 String find = "chat";

我必须检查新单词是否已经存在于现有单词中

情况看起来像,

if(test has find)
      //go something
else
      //go somethig

在java中是否可能


共 (5) 个答案

  1. # 1 楼答案

    那-

    if(test.indexof(find)>-1){
      ... // found
    }
    
  2. # 2 楼答案

    执行以下代码。如果有大数据文件或字符串,请使用正则表达式。这是最好的

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexMatches
    {
        public static void main( String args[] ){
    
          // String to be scanned to find the pattern.
          String line = "voiceemailchat";
          String pattern = "chat";
    
          // Create a Pattern object
          Pattern r = Pattern.compile(pattern);
    
          // Now create matcher object.
          Matcher m = r.matcher(line);
          if (m.find( )) {
             System.out.println("Found the value");
          } else {
             System.out.println("NO MATCH");
          }
       }
    }
    
  3. # 3 楼答案

    试试这个String#indexOf()

    if(test.indexOf(find)!=-1){
       ... // value is found
    }
    
  4. # 4 楼答案

    是的,有可能:

    String test = "voiceemailchat";
    String find = "chat";
    
    if(test.contains(find)) {
        //string found
    }
    
  5. # 5 楼答案

    作为第一步,请始终查看Javadocs。有关String的信息,请单击here

    您可以使用以下任何方法:containsindexOf