【问题标题】:Returning a string minus a specific character between specific characters返回字符串减去特定字符之间的特定字符
【发布时间】:2015-04-07 17:08:06
【问题描述】:

我正在学习 Java CodeBat 练习。 Here is the one I am stuck on:

在字符串中查找诸如“zip”和“zap”之类的模式——长度为 3,以 'z' 开头并以 'p' 结尾。返回一个字符串,其中对于所有此类单词,中间字母都消失了,因此“zipXzap”产生“zpXzp”。

这是我的代码:

    public String zipZap(String str){

    String s = ""; //Initialising return string
    String diff = " " + str + " "; //Ensuring no out of bounds exceptions occur

    for (int i = 1; i < diff.length()-1; i++) {
        if (diff.charAt(i-1) != 'z' &&
                diff.charAt(i+1) != 'p') {
            s += diff.charAt(i);
        }
    }
    return s;
}

这对他们中的一些人来说是成功的,但对其他人来说却不是。对于某些示例字符串,&amp;&amp; 运算符的作用似乎类似于||;也就是说,很多我想保留的角色都没有保留。我不确定我将如何修复它。

如果您愿意,请朝正确的方向轻推!我只是需要一个提示!

【问题讨论】:

  • 这个问题太模糊了,没法回答

标签: java string for-loop char charat


【解决方案1】:

其实情况正好相反。你应该这样做:

if (diff.charAt(i-1) != 'z' || diff.charAt(i+1) != 'p') {
    s += diff.charAt(i);
}

相当于:

if (!(diff.charAt(i-1) == 'z' && diff.charAt(i+1) == 'p')) {
    s += diff.charAt(i);
}

【讨论】:

    【解决方案2】:

    这听起来像是对正则表达式的完美使用。

    正则表达式"z.p" 将匹配任何以z 开头、中间有任何字符并以p 结尾的三个字母标记。如果你要求它是一个字母,你可以改用"z[a-zA-Z]p"

    所以你最终得到了

    public String zipZap(String str) {
        return str.replaceAll("z[a-zA-Z]p", "zp");
    }
    

    顺便说一句,这通过了所有测试。

    你可以说这个问题是关于原始字符串操作的,但我认为这会成为一个更好的教训:适当地应用正则表达式是一项非常有用的技能!

    【讨论】:

    • 在“掌握”原始字符串操作之前了解正则表达式会更好吗?你经常使用正则表达式吗?
    • 我使用正则表达式的频率远高于原始字符串操作。事实上,我几乎从不使用原始字符串操作,除非我在库的内部编写了坚韧不拔的细节,或者我正在一个非常紧凑的循环中处理一段代码,这两种情况都不是很常见.
    • 我明白了。我想我应该能够流利地做到这两点;我不想处于一个我已经掌握了一个并且严重缺乏另一个知识的位置。
    • 不同之处在于原始字符串操作主要是常识和一点数学。正则表达式......嗯......可能挽救生命xkcd.com/208 :)
    【解决方案3】:
    public String zipZap(String str) {
        //If bigger than 3, because obviously without 3 variables we just return the string.
        if (str.length() >= 3)
        {
          //Create a variable to return at the end.
          String ret = "";
          //This is a cheat I worked on to get the ending to work easier.
          //I noticed that it wouldn't add at the end, so I fixed it using this cheat.
          int minusAmt = 2;
          //The minus amount starts with 2, but can be changed to 0 when there is no instance of z-p.
          for (int i = 0; i < str.length() - minusAmt; i++)
          {
            //I thought this was a genius solution, so I suprised myself.
            if (str.charAt(i) == 'z' && str.charAt(i+2) == 'p')
            {
              //Add "zp" to the return string
              ret = ret + "zp";
              //As long as z-p occurs, we keep the minus amount at 2.
              minusAmt = 2;
              //Increment to skip over z-p.
              i += 2;
            }
            //If it isn't z-p, we do this.
            else
            {
              //Add the character
              ret = ret + str.charAt(i);
              //Make the minus amount 0, so that we can get the rest of the chars.
              minusAmt = 0;
            }
          }
          //return the string.
          return ret;
        }
        //If it was less than 3 chars, we return the string.
        else
        {
          return str;
        }
      }
    

    【讨论】:

    • 你觉得这个循环怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2021-10-07
    • 2016-05-05
    相关资源
    最近更新 更多