用特定的单词列表替换字符串

时间:2019-04-17 17:34:22

标签: java string replace

用单词列表替换给定字符串在给定索引处。 问题陈述如下,有人可以给我一些直觉或想法如何进行此操作吗?

//A replacement class similar to Linked List
class Replacement {
        int start;
        String before;
        String after;

    //Method to replace the words
    public static String replaceRanges(String text, List<Replacement> replacements) {
        //TODO your code here
        return null;
    }

}

/* below is the example of the problem
    Example #1:
    Input: 
    text = "num foo"
    replacements = [
    {start: 0, before: "num", after: "String"},
    {start: 4, before: "foo", after: "bar"}
    ]
    Output:
    replaceRanges(text, replacements) returns:
    "String bar"
    Example #2: Input: text = "num_fooBar", Output: "String_barBar"
*/

2 个答案:

答案 0 :(得分:0)

如果您的替换项从最小索引到最高索引排序,则可以从最后到第一顺序进行迭代,并在输入字符串中搜索子字符串,并在匹配时替换它们

public static String replaceRanges(String text, List<Replacement> replacements) {
    StringBuilder s = new StringBuilder(text);

    for (int i = replacements.size() - 1; i>=0; i--) {
        Replacement r = replacements.get(i);
        int begin = r.start;
        int end = r.start + r.before.length();
        if (begin >= 0 && begin < s.length() && end >= 0 && end <= s.length()) {
            if (s.substring(begin, end).equals(r.before)) {
                s.replace(begin, end, r.after);
            }
        }
    }

    return s.toString();
}

如果您的列表未排序,则需要先使用Collections.sort()对其进行排序。

我使用以下代码进行测试:

public static void main(String[] args) {
    List<Replacement> replacements = List.of(
            new Replacement(0, "num", "String"), 
            new Replacement(4, "foo", "bar"));

    System.out.println(replaceRanges("num foo", replacements)); // Output: String bar
    System.out.println(replaceRanges("num_fooBar", replacements)); // Output: String_barBar
    System.out.println(replaceRanges("num_f", replacements)); // Output: String_f
    System.out.println(replaceRanges("", replacements)); // Output: 
    System.out.println(replaceRanges("foonum", replacements)); // Output: foonum
}

答案 1 :(得分:0)

您可以一一替换原来的字符串,并记住必须将start的位置移位(因为可以将较小的子字符串替换为较大的子字符串)

public String replaceRanges(String text, List<Replacement> replacements) {
    for(int i = 0; i < replacements.size(); i++) {
        Replacement replacement = replacements.get(i);
        String firstPart = text.substring(0, replacement.start);
        String secondPart = text.substring(replacement.start, text.length());
        String updatedSecondPart = secondPart.replace(replacement.before, replacement.after);
        text = firstPart + updatedSecondPart;
        updateStart(i + 1, replacements, updatedSecondPart.length() - secondPart.length());
    }
    return text;
}

privat void updateStart(int startIndex, List<Replacement> replacements, int shift) {
    for( int i = startIndex; i < replacements.size(); i++) {
        Replacement r = replacements.get(i);
        r.start += shift;
    }
}

使用此方法,您可以处理:

Replacement r1 = new Replacement(0, "hi", "Hello");
Replacement r2 = new Replacement(2, "lo", "p");
Sting result = replaceRanges("hi louie!", asList(r1, r2)); //result = 'Hello puie!'