0

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-

Community
  • 1
  • 1
mazipan
  • 183
  • 2
  • 10

3 Answers3

2

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
Boann
  • 48,794
  • 16
  • 117
  • 146
  • thank you, this is what I want. but this is problem a long time ago. I even forget about this problem. thanks @Boann – mazipan Mar 04 '16 at 06:42
0

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)()
Cory Koch
  • 470
  • 4
  • 8
0

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

Bnrdo
  • 5,325
  • 3
  • 35
  • 63
  • "SUM(test)+SUM(test2)+test3" is exactly not what OP wants. – Boann Nov 27 '13 at 03:26
  • I think that is what the OP wants. – Bnrdo Nov 27 '13 at 03:29
  • He wants "SUM(test)+SUM(testA)+testB". – Boann Nov 27 '13 at 03:31
  • If that is the case, I agree with first part of your answer to process the strings in order of decreasing length. The OP's question for me is blurry though, and how I understand it, he wants `SUM(test)+SUM(test2)+test3`. Im gonna upvote your answer for this (side note : I'm not the one who gave that downvote). – Bnrdo Nov 27 '13 at 03:58