【问题标题】:Java String checkJava 字符串检查
【发布时间】:2014-11-03 21:28:43
【问题描述】:

我正在检查句子中的“of”是否可用。我正在尝试检查 java 字符串。 page是一个String变量,代码在这里

page="Paint, Body & Trim : Body : Moldings : Sunroof";



if(page.contains("of"))
{
}
else
{
}

上面示例天窗中的问题是“of”,因此循环为真。但我不想使用带有“of”的词。请帮帮我。

【问题讨论】:

    标签: java string if-statement


    【解决方案1】:

    改一下

    if(page.contains("of"))
    

    if(page.contains(" of "))
    

    编辑:只考虑句子是否以“of”开头或结尾:

    if(page.contains(" of ") || page.startsWith("of ") || page.endsWith(" of"))
    

    【讨论】:

    • 如果 "of" 位于字符串的开头,即开头没有字符串,这将失败。
    • 如果“of”在标点符号之前,这将失败。
    • @ZaheerAhmed 它怎么会返回真正的天窗和" as page.startsWith("of ")??
    【解决方案2】:

    你可以这样做。使用equals() 而不是contains()

    String page="Paint, Body & Trim : Body : Moldings : Sunroof";
      for (String i:page.split(" ")){
            if("of".equals(i)){
    
            }
        }
    

    【讨论】:

      【解决方案3】:

      您可以使用Scanner" : " 分隔符:

      String page="Paint, Body & Trim : Body : Moldings : Sunroof";
      
      boolean contains(Scanner scan, word){
          scan.useDelimiter(" : ");
          while(in.hasNext())
              if(in.next().equals(word) return true;
          return false;
      }
      

      然后像这样拨打电话System.out.println(contains(new Scanner(page), "of");

      这是打印false

      【讨论】:

        【解决方案4】:

        尝试if(page.contains(" of ")),这样它只会使用单词“of”而不是带有该子字符串的字符串。

        【讨论】:

          【解决方案5】:

          最好的方法是使用正则表达式:

          page.matches(".*\\b[Oo][Ff]\\b.*")
          

          .* 表示“任何字符零次或多次”。 \\bword boundary[Oo] 表示字符'O',大写或小写。

          这里有一些测试用例:

          String page = "Paint, Body & Trim : Body : Moldings : Sunroof";
          System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // false
          page = "A piece of cake";
          System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
          page = "What I'm made of";
          System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
          page = "What I'm made of.";
          System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
          page = "What I'm made of, flesh";
          System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
          page = "Of the Night";
          System.out.println(page.matches(".*\\b[Oo][Ff]\\b.*")); // true
          

          在“of”在开头、结尾、标点符号之前...

          【讨论】:

            【解决方案6】:

            或者如果你只看整个单词的另一种方式:

            String page="Paint, Body & Trim : Body : Moldings : Sunroof";
            StringTokenizer st2 = new StringTokenizer(str, " ");//space
            
                    while (st2.hasMoreElements()) {
                        if((st2.nextElement().equals("of")){
                          //whatever..
                         }
                    }
            

            【讨论】:

              【解决方案7】:

              您需要在这里使用正则表达式,否则您需要添加许多检查:

              System.out.println(str.matches(".*\\bof\\b.*"));   //will return true
              

              这里是Demo

              【讨论】:

                【解决方案8】:

                在搜索字符串前后添加空格, 即

                if(page.contains(" of ")){
                }
                

                另一种方法是用空格分隔符分割句子,但这里你需要循环遍历字符串数组。

                【讨论】:

                  【解决方案9】:

                  你可以的

                  if (page.matches(".*\\bof\\b.*")) {
                  

                  【讨论】:

                    猜你喜欢
                    • 2016-01-14
                    • 2014-07-29
                    • 1970-01-01
                    • 2017-12-04
                    • 2016-02-26
                    • 2012-07-14
                    • 1970-01-01
                    • 2014-08-26
                    • 2011-12-28
                    相关资源
                    最近更新 更多