mazipan
mazipan

Reputation: 183

Replace Array String with Other Array String

same topic : replace String with another in java

I want to replace String Replace = "SUM(NewCounter)+SUM(NewCounter2)+NewCounter3";

I have Array A = {NewCounter, NewCounter2, NewCounter3}
say I have Array B = {test, testA, testB}

I want to replace it with array A with array B in String Replace.
I try to use method ReplaceAll(A.get(index), B.get(index));

Problem is:

NewCounter2 is Read by system "NewCounter"+2
so I have result = String Replace = "SUM(test)+SUM(test2)+test3";

I try to use ' in Character NewCounter, it will be Array A = {'NewCounter', 'NewCounter2', 'NewCounter3'}

but I must change String Replace Before like this :

String Replace = "SUM('NewCounter')+SUM('NewCounter2')+'NewCounter3'";

Is there other way to me ??? I don't want to change String before...

Thanksfull,

-mazipan-

Upvotes: 0

Views: 739

Answers (3)

Boann
Boann

Reputation: 50041

The simplest solution for simultaneous replacement is to process the strings in order of decreasing length. This will do the replacements correctly:

A = {NewCounter3, NewCounter2, NewCounter}
B = {testB, testA, test}

This technique won't work if any of the search strings could match the replacement strings, however.

Edit: For the general case, I've written this:

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

Example usage:

String s = "SUM(NewCounter)+SUM(NewCounter2)+NewCounter3";

s = simultaneousReplace(s,
    new String[] { "NewCounter", "NewCounter2", "NewCounter3" },
    new String[] { "test", "testA", "testB" }
);

System.out.println(s);

Output:

SUM(test)+SUM(testA)+testB

Upvotes: 2

Bnrdo
Bnrdo

Reputation: 5474

Apache's commons-lang StringUtils#replaceEach handles these problems elegantly.

Runnable Code :

public static void main(String [] args) {
     String replace = "SUM(NewCounter)+SUM(NewCounter2)+NewCounter3";
     String [] a = { "NewCounter", "NewCounter2", "NewCounter3" };
     String [] b = { "test", "testA", "testB" };

     System.out.println(StringUtils.replaceEach(replace, a, b));
}

Will give you

SUM(test)+SUM(test2)+test3

Upvotes: 0

Cory Koch
Cory Koch

Reputation: 514

If SUM is just a String and not a method then you can use:

String Replace = "SUM(" + NewCounter + ")SUM(" + NewCounter2 +")" + NewCounter3;

If Sum is a method then you can use

 String Replace = SUM(NewCounter) + SUM(NewCounter2) + NewCounter3;

Although for the second one you may have to cast/convert to a string by surrounding it with

().toString()

or by adding

(String)()

Upvotes: 0

Related Questions