【问题标题】:String matches method to get words beginning with specified letters in Java字符串匹配方法以获取Java中以指定字母开头的单词
【发布时间】:2013-11-13 02:51:16
【问题描述】:

我正在使用 ArrayList 来保存字符串列表,并且我正在遍历 ArrayList 并尝试返回以字符串开头的单词(用户输入)。

我的代码:

private void doP( ArrayList <String> words, String cmd3 )
{
    for( int i = 0; i < words.size(); i++ )
    {
        if( words.get( i ).matches( cmd3 + "(.*)" ))
        {
            tempString.add( words.get( i ));
            System.out.println( "H:" + words.get( i ));
        }
    }

    System.out.println( tempString );

如果我将 ( cmd3 + "(.* )" ) 替换为 ( "th(.*)" ),则代码可以正常工作。 我认为这里的问题是格式,我真的想不通。

抱歉使用非描述性术语,我刚开始编程 2 个月前...

TLDR;

if( words.get( i ).matches( cmd3 + "(.*)" ))

这就是问题所在,特别是 (cmd3 + (.*))。我在这方面的逻辑是,用户在末尾输入 (.*) 的任何字符串都应该有效,例如,如果用户键入“ABCD”,那么我应该有:

if( words.get( i ).matches( "ABCD(.*)" ))

谢谢!

PS:startsWith() 方法完成了这项工作,这对我来说是一个非常小的错误。感谢所有回答的人。

【问题讨论】:

  • The code works properly if I replace ( cmd3 + "(.* )" ) with ( "th(.*)" ) 证明它。向我们展示输入和预期/实际输出。
  • 您知道您正在创建一个正则表达式来匹配某些内容吗?此外,您的正则表达式说(对于 ABCD)您的列表中必须有一个与 ABCD+anythingelse 匹配的元素
  • @Sotirios Delimanos - 我给了我的老师一个 file.txt,在一部分中我必须创建一个 ArrayList,其中是一个以字母/字母开头的单词列表 t , th 分别。

标签: java string arraylist iteration


【解决方案1】:

根据您的描述,您似乎正在尝试重新发明startsWith 方法。

试试吧

if( words.get(i).startsWith(cmd3))

【讨论】:

    【解决方案2】:

    这是因为 .matches 使用的是正则表达式,所以它正在寻找 cmd3 的值。

    我不想对正则表达式过于详细而让你感到困惑,但带有 (.*) 的部分可以工作,因为它基本上是添加“零个或多个任意字符”

    我认为 .startWith(cmd3) 会更适合您。

    您可以阅读 .starsWith() here

    【讨论】:

      【解决方案3】:

      您可以使用startsWith(str) 方法:

      private static void doP(ArrayList<String> words, String cmd3)
      {
          ArrayList<String> tempString = new ArrayList<String>();
          for (int i = 0; i < words.size(); i++)
          {
              if (words.get(i).startsWith(cmd3))
              {
                  tempString .add(words.get(i));
                  System.out.println("H:" + words.get(i));
              }
          }
      
          System.out.println(tempString.toString());
      }
      
      public static void main(String[] args) throws FileNotFoundException
      {
          ArrayList<String> strings = new ArrayList<String>();
          strings.add("ABCDE");
          strings.add("ABCDEFG");
          strings.add("BCDE");
          doP(strings, "ABC");
      }
      

      【讨论】:

      • 我应该在我的问题中更具体一些,但基本上我必须将一个文件排序到一个 ArrayList 中,其中包含一个我必须分开的单词列表。特别是带有字母 t 和 th 的单词。而且我认为 startsWith 只接受字符串中的第一个字母,所以 startsWith( "th" ) 不起作用。除非我错了。
      • PS:好吧,我的错,你是对的,这是我在代码中的错误。我一开始有startsWith(),但由于有点混乱而没有得到正确的输出,但是谢谢。
      【解决方案4】:

      我尝试运行您在此处发布的方法,它似乎没有任何问题,我只需将 tempString 的声明放在您的方法中

      import java.util.ArrayList;
      public class Test {     
      public static void main(String a[] ) {   
        ArrayList<String> arr = new ArrayList<String>(); 
        arr.add("ABCD");
        arr.add("EFGH");
        arr.add("ABYZ");
        doP(arr, "AB");
      }
      
      private static void doP( ArrayList <String> words, String cmd3 )
      {
          ArrayList<String> tempString = new ArrayList<String>(); 
          for( int i = 0; i < words.size(); i++ )
          {
              if( words.get( i ).matches( cmd3 + "(.*)" ))
              {
                  tempString.add( words.get( i ));
                  System.out.println( "H:" + words.get( i ));
              }
          }
      
          System.out.println( tempString );
      }
      }
      

      运行它会产生以下输出

      H:ABCD
      H:ABYZ
      [ABCD, ABYZ]
      

      这不是您在输出中所期望的吗?那么这里真正的问题是什么?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多