looping through a string and replacing values within that string

Can you think of a quick way of searching (looping) through a string and replacing a given occurrence of a variable in that string with another value?!

 

As in I could have any number of values like [1], [2]  and I want to loop through and replace these values with certain values.

LVL 1
pronaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
s = s.replaceAll("x", "y");
0
CEHJCommented:
x=to replace
y = replacement
0
BogoJokerCommented:
Hi pronane,

String test = "Hello, a, b, c";
String[] replaceThese = {"a", "b", "c"};
String[] replaceWith = {"x", "y", "z" };
for (int i = 0; i < replaceThese.length; i++)
  test = test.replaceAll(replaceThese[i], replaceWith[i]);

That loops through the array of what you want to change and replaces it with the cooresponding same index in relaceWith.

Joe P
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

pronaneAuthor Commented:
so as it goes through that will replace, a with x, b with y and c with z etc is that correct?


the thing is the second string array, i.e. replaceWith may not always have the same values....
0
Mayank SAssociate Director - Product EngineeringCommented:
>> so as it goes through that will replace, a with x, b with y and c with z etc is that correct?

Yes.

>> thing is the second string array, i.e. replaceWith may not always have the same values

That's only an example. You can replace the final values with whatever you want, once you have those values in the first place.
0
CEHJCommented:
>>may not always have the same values....

It will use whatever values it does have
0
BogoJokerCommented:
Exactly.
Test will be this after the for loop:
"Hello, x, y, z"

So you can change the contents of replaceThese and replaceWith as you wish!
The values don't even need to be similar. This would work too:
replaceThese = {"a", "b", "c"}
replaceWith = {"123", "lol", "Joe Was Here"}

Then test would be:
"Hello, 123, lol, Joe Was Here"

=)

Joe P
0
CEHJCommented:
You have to be careful about what characters are in either String parameter - they are both regexes, so any reserved regex characters need to be escaped
0
pronaneAuthor Commented:
see the replace with values will be picked up on the fly....dynamically i mean.
0
Mayank SAssociate Director - Product EngineeringCommented:
>> see the replace with values will be picked up on the fly....dynamically i mean.

Not a problem :) can we know how they will be picked up?
0
CEHJCommented:
>>values will be picked up on the fly.

Doesn't matter
0
mrigankCommented:
One quick reminder

String[] replaceThese = {"a", "x", "y"};
String[] replaceWith = {"x", "y", "a" };

for a String axy would give aaa
The  n+1 th loop could undo what the nth loop  did.

or should the output be xya ?


0
pronaneAuthor Commented:
i tried the above code but the message string at the end is completely different than what i expected,

[xx] [xy] [xz]

is the output string given:

   String[] replaceThese = {"[%1]", "[%2]", "[%3]"};
     String[] replaceWith = {"x", "y", "z"};

i also want to get rid of the square brackets....once the values within are replaced, but this doesnt appear to be happening....
0
CEHJCommented:
Try

String[] replaceThese = {"\\[%1\\]", "\\[%2\\]", "\\[%3\\]"};
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
0
pronaneAuthor Commented:
these values are all rendered in an html page so if i populate the replaceWith string array like such:

String[] replaceWith = {"x <br/>", "y <br/>", "z <br/>"};

will the x, y and z values be moved onto a new line, or do certain characters need to be added like above for regex characters?!?!
0
pronaneAuthor Commented:
that should be within a jsp actually...
0
Mayank SAssociate Director - Product EngineeringCommented:
Its not a problem to do it in a JSP - you can do it within the <% and %>
0
pronaneAuthor Commented:
the above change you advised is now giving me a:
java.util.regex.PatternSyntaxException:
0
CEHJCommented:
>>
 the above change you advised is now giving me a:
java.util.regex.PatternSyntaxException:
>>

Which one?

System.out.println("xxx[%3]".replaceAll("\\[%3\\]", "!"));

prints

xxx!

fine
0
pronaneAuthor Commented:
it give a problem with the final  "\"
0
CEHJCommented:
You can test an invalid expression by

Pattern.compile("\\[%3\\]");

If no expression is thrown, it's valid (that one is)

0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.