简体   繁体   English

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

[英]Replace Strings with specific list of words

Replace a given string at given index with list of words.用单词列表替换给定索引处的给定字符串。 The problem statement goes below, can someone give me some intuition or idea how to proceed on this?问题陈述如下,有人能给我一些直觉或想法如何继续吗?

//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"
*/

If you have your replacements sorted from smallest index to highest, you can iterate the list from last to first and search for substrings in input string and replace them if they match如果您的替换从最小索引到最高排序,您可以从最后到第一个迭代列表并在输入字符串中搜索子字符串,如果它们匹配则替换它们

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();
}

If your list is not sorted, you need to sort it first using Collections.sort() .如果您的列表未排序,则需要先使用Collections.sort()对其进行排序。

I used this code for testing:我使用此代码进行测试:

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
}

You could replace your original string one by one and keep in mind that you have to shift the start position (because you could replace the small substring to the bigger substring)你可以一一替换你原来的字符串,记住你必须移动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;
    }
}

Using this method you can process:使用此方法,您可以处理:

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!'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM