【问题标题】:3 consecutive chars with letters in between3个连续的字符,中间有字母
【发布时间】:2014-02-26 04:48:32
【问题描述】:

我试图以相同的顺序查找单词中包含字符“d”+“a”+“t”的所有单词,但它们之间可能有空格。例如..冗余...它有d、a和t,但系列之间有一个字母。我现在有这个,我不知道还能做什么。

static void mainTest (){

  String[] words = WordTool.getDictionary ();
  int count = 0;
  System.out.println();

  for (int j = 0 ; j < words.length ; j++){

     if ( words[j].contains("d" + "a" + "t")){
         System.out.println(words[j]);
         count ++;
     }

  }

【问题讨论】:

  • 提示:indexOf("t") &gt; indexOf("a") &gt; indexOf("d"),它们都不等于-1
  • @RohitJain 逻辑很好,但是由于严格大于的传递属性,您只需要确保d 的索引大于或等于0。

标签: java if-statement char


【解决方案1】:

您也可以使用正则表达式,例如:

System.out.println("redundant".matches(".*d.*a.*t.*")); //prints true

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 2016-09-02
    • 2011-09-06
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多