java - java字符串自定义替换的优雅解决方案

标签 java regex string

我解决了下面列出的问题,它工作正常,但看起来笨重且效率不高。我正在寻找改进它并获得更优雅的解决方案的方法,有什么建议我该如何改进它?任何建议表示赞赏。谢谢!

问题: 给定一个字符串,返回一个字符串,其中每次出现的小写单词“is”都被替换为“is not”。 “is”一词前面或后面不应紧跟一个字母 - 因此,例如“this”中的“is”不算在内。

测试:

    notReplace("is test") → "is not test" 
    notReplace("is-is") → "is not-is not" 
    notReplace("This is right") → "This is not right"
    notReplace("This is isabell") → "This is not isabell" 
    notReplace("")→ ""
    notReplace("is") → "is not" 
    notReplace("isis") → "isis"
    notReplace("Dis is bliss is") → "Dis is not bliss is not"
    notReplace("is his") → "is not his"    
    notReplace("xis yis") → "xis yis" 
    notReplace("AAAis is") → "AAAis is not"

我的解决方案:

    public static String notReplace(String str) {
    String result="";
    int begin = 0;
    if (str.equals("is"))
         return "is not";
    int index = str.indexOf("is");
    if (index==-1) 
         return str;
    while (index>-1){
          if (index+begin==0 && !Character.isLetter(str.charAt(index+2))){
              result += "is not";
            begin = index+2; 
            index = str.substring(begin).indexOf("is");
          }
          else if (index+begin==0 && Character.isLetter(str.charAt(index+2))){
              result += str.substring(begin,begin+index)+"is";
              begin += index+2; 
              index = str.substring(begin).indexOf("is");
          }
          else if (index+begin == str.length()-2 && !Character.isLetter(str.charAt(index+begin-1))){
              result += str.substring(begin, begin+index)+"is not";
              return result;
          }
          else if(!Character.isLetter(str.charAt(index+begin-1))&&!Character.isLetter(str.charAt(index+begin+2))){
              result += str.substring(begin,begin+index)+"is not";
              begin += index+2; 
              index = str.substring(begin).indexOf("is");
          }
          else {
              result += str.substring(begin,begin+index)+"is";
              begin += index+2; 
              index = str.substring(begin).indexOf("is");
          }
      }
      result += str.substring(begin);
      return result;
}

最佳答案

此解决方案适用于您的大多数示例:

public String notReplace(String str) {
    // Add surrounding whitespace in case of an "is" at the beginning or end
    str = " " + str + " ";
    // Do replacement
    String result = str.replaceAll(" is ", " is not ");
    // Other replacements...
    // result = result.replaceAll("", "");

    return result.trim(); // Remove added whitespaces again using trim()
}

对于未替换为此代码的示例,您需要添加一些额外的代码行。或者研究正则表达式 - 正如 still_learning 所说。

希望这有帮助。

关于java - java字符串自定义替换的优雅解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33987692/

相关文章:

java - 立即停止线程中的 Runnable

java - 如何将ListView放入Fragment中

c++ - 存储为字符串的大量 100 位数字的除法

javascript - 带点的字符串的正则表达式

java - 在之前更改背景颜色后禁用时,JTextField 的背景颜色不会变为 'grayed out'

java - 使用 setDefaultUncaughtExceptionHandler() 时如何强制正常崩溃行为?

java - 验证字符串没有非法字符

asp.net - 正则表达式排除双空格

ruby - 我想用 ruby​​ 在一行文本中搜索给定的单词

python 正则表达式: expression to match number and letters