如何替换 Java 中单词的所有实例,只要它们不是其他单词的一部分?



我是Java初学者,想问一下如何替换单词"是";在一个字符串中;不是";只有当它前面或后面没有另一个字母(数字和符号很好(,并且不是另一个单词的一部分时。

例如,我想将单词";是";用";不是";在每个字符串中,但如果不更改包含"的所有单词,则很难做到这一点;是";例如";这个";或";"小姐";。

目前,当我试图使用一个像"这样的词时,我的代码陷入了无限循环;这个";调用它。(请参阅下面的示例输出(例如,这是我的代码:

public static String isReplace(String str) {
    String newStr = "";
    for (int i = 0; i < str.length(); i++) {
        
        if (str.charAt(i) == ('i') && str.charAt(i + 1) == ('s')) {
            if (!(Character.isLetter(str.charAt(i + 2)))) {
                str = str.replace("is", "is not");
            }
        }
        //break;
    }
    return str ; // FIX ME
}
isReplace("This is good")); //should give me "This is not good"
isReplace("is-is")); //should give me "is not-is not"
isReplace("My favorite food is9ie")) // should give me "My favorite food is not9ie"

刚刚添加了对现有逻辑的更多细化。

public class P1 {
    public static String isReplace(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char current = str.charAt(i);
            //if last character just add it to the buffer
            if(i == str.length()-1){
                sb.append(current);
            }else{
                char next = str.charAt(i+1);
                if(current == 'i' && next == 's'){
                    //check is preceding character is not letter and following character is not letter
                    if((i == 0 || !Character.isLetter(str.charAt(i-1))) &&
                            ((i+2 == str.length() || !Character.isLetter(str.charAt(i+2))))){
                        sb.append("is not");
                        i++;  // to skip reading s in next iteration
                    }else{
                        sb.append(current);
                    }
                }else{
                    sb.append(current);
                }
            }
        }
        return sb.toString() ;
    }
    public static void main(String[] args) {
        System.out.println(isReplace("This is good"));
        System.out.println(isReplace("is-is"));
        System.out.println(isReplace("My favorite food is9ie"));
    }
}
This is not good
is not-is not
My favorite food is not9ie

可能最简单的方法是使用Regex。利用该搜索输入;是";没有字母环绕的。

str.replaceAll("(?<![a-zA-Z])is(?![a-zA-Z])", "is not")

下面是一个工作示例:

  public class Main {
  public static String isReplace(String string) {
    return string.replaceAll("(?<![a-zA-Z])is(?![a-zA-Z])", "is not");
  }
  public static void main(String[] args) {
    System.out.println(isReplace("This is good"));
    System.out.println(isReplace("My favorite food is9ie"));
    System.out.println(isReplace("is#is"));
  }
}

输出:

This is not good
is not#is not
My favorite food is not9ie

Regex解释道:

  (        Open the first capturing group
  ?<!      Negative lookbehind. This will look before the search results and check that none of the following characters are there.
  [a-zA-Z] Character set of all letters.
  )        Close the first capturing group.
  is       Searches for "is".
  (        Open the second capturing group.
  ?!       Negative lookahead. This will look ahead of the search results and check that none of the following characters are there.
  [a-zA-Z] Character set of all letters.
  )        Close the second capturing group.

相关内容

  • 没有找到相关文章

最新更新