• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

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.

0
pronane
Asked:
pronane
  • 10
  • 7
  • 3
  • +2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 7
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now