Replace multiple substrings at once

前端 未结 5 1548
予麋鹿
予麋鹿 2020-12-05 20:56

Say I have a file, that contains some text. There are substrings like \"substr1\", \"substr2\", \"substr3\" etc. in it. I need to replace all of those substrings with some o

5条回答
  •  抹茶落季
    2020-12-05 21:43

    First, a demonstration of the problem:

    String s = "I have three cats and two dogs.";
    s = s.replace("cats", "dogs")
        .replace("dogs", "budgies");
    System.out.println(s);
    

    This is intended to replace cats => dogs and dogs => budgies, but the sequential replacement operates on the result of the previous replacement, so the unfortunate output is:

    I have three budgies and two budgies.

    Here's my implementation of a simultaneous replacement method. It's easy to write using String.regionMatches:

    public static String simultaneousReplace(String subject, String... pairs) {
        if (pairs.length % 2 != 0) throw new IllegalArgumentException(
            "Strings to find and replace are not paired.");
        StringBuilder sb = new StringBuilder();
        int numPairs = pairs.length / 2;
        outer:
        for (int i = 0; i < subject.length(); i++) {
            for (int j = 0; j < numPairs; j++) {
                String find = pairs[j * 2];
                if (subject.regionMatches(i, find, 0, find.length())) {
                    sb.append(pairs[j * 2 + 1]);
                    i += find.length() - 1;
                    continue outer;
                }
            }
            sb.append(subject.charAt(i));
        }
        return sb.toString();
    }
    

    Testing:

    String s = "I have three cats and two dogs.";
    s = simultaneousReplace(s,
        "cats", "dogs",
        "dogs", "budgies");
    System.out.println(s);
    

    Output:

    I have three dogs and two budgies.

    Additionally, it is sometimes useful when doing simultaneous replacement, to make sure to look for the longest match. (PHP's strtr function does this, for example.) Here is my implementation for that:

    public static String simultaneousReplaceLongest(String subject, String... pairs) {
        if (pairs.length % 2 != 0) throw new IllegalArgumentException(
            "Strings to find and replace are not paired.");
        StringBuilder sb = new StringBuilder();
        int numPairs = pairs.length / 2;
        for (int i = 0; i < subject.length(); i++) {
            int longestMatchIndex = -1;
            int longestMatchLength = -1;
            for (int j = 0; j < numPairs; j++) {
                String find = pairs[j * 2];
                if (subject.regionMatches(i, find, 0, find.length())) {
                    if (find.length() > longestMatchLength) {
                        longestMatchIndex = j;
                        longestMatchLength = find.length();
                    }
                }
            }
            if (longestMatchIndex >= 0) {
                sb.append(pairs[longestMatchIndex * 2 + 1]);
                i += longestMatchLength - 1;
            } else {
                sb.append(subject.charAt(i));
            }
        }
        return sb.toString();
    }
    

    Why would you need this? Example follows:

    String truth = "Java is to JavaScript";
    truth += " as " + simultaneousReplaceLongest(truth,
        "Java", "Ham",
        "JavaScript", "Hamster");
    System.out.println(truth);
    

    Output:

    Java is to JavaScript as Ham is to Hamster

    If we had used simultaneousReplace instead of simultaneousReplaceLongest, the output would have had "HamScript" instead of "Hamster" :)

    Note that the above methods are case-sensitive. If you need case-insensitive versions it is easy to modify the above because String.regionMatches can take an ignoreCase parameter.

提交回复
热议问题