We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# looping through a string and replacing values within that string

on
Medium Priority
178 Views
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.

Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Top Expert 2016

Commented:
s = s.replaceAll("x", "y");
CERTIFIED EXPERT
Top Expert 2016

Commented:
x=to replace
y = replacement

Commented:
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

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....
Principal Technologist
CERTIFIED EXPERT

Commented:
>> 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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>may not always have the same values....

It will use whatever values it does have

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Commented:
see the replace with values will be picked up on the fly....dynamically i mean.
Principal Technologist
CERTIFIED EXPERT

Commented:
>> 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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>values will be picked up on the fly.

Doesn't matter

Commented:
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 ?

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....
CERTIFIED EXPERT
Top Expert 2016
Commented:
Try

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

Not the solution you were looking for? Getting a personalized solution is easy.

CERTIFIED EXPERT
Top Expert 2016

Commented:

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

Commented:
that should be within a jsp actually...
Principal Technologist
CERTIFIED EXPERT

Commented:
Its not a problem to do it in a JSP - you can do it within the <% and %>

Commented:
the above change you advised is now giving me a:
java.util.regex.PatternSyntaxException:
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
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

Commented:
it give a problem with the final  "\"
CERTIFIED EXPERT
Top Expert 2016

Commented:
You can test an invalid expression by

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

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

CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile