-1

Suppose I have a string say "Hello ::2130837661:: stackoverflow, How ::2130837664:: are you?"

And I have a predefined Integer Arraylist like [ 2130837661, 2130837662, 2130837663, 2130837664, 2130837665, 2130837666, 2130837667, 2130837668 ]

I want to check if given string contains integer from that arraylist, it should replace by new word say "EXAMPLE".

For example above string(after replacement) will be :

"Hello EXAMPLE stackoverflow, How EXAMPLE are you?"

Any feasible solution to do this?

Thanks in advance.

Ambar Jain
  • 508
  • 5
  • 11

1 Answers1

0

Simply iterate over the ArrayList and (I assume you have a Map from these integers to Strings) use String.replace() to replace your integer with the respective replacement.

for(Integer i: arrayList)
{
  yourString.replace("::"+i+"::", replacement);
}
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
  • This has a very poor performance and does not work because Java returns a new string! – Markus Jun 06 '16 at 06:52
  • Just look at the OPs question. He has an `ArrayList` which consists of specific integers. If we were to simply use those integers hardcoded in the regex as your solution, what is the point of having this `ArrayList`? If Java returns a new `String`, simply assign it to the original `String` what is the issue? – Abdul Fatir Jun 06 '16 at 06:56
  • I hope, he can interpret the regexp example and adjust it for his needs. As software dev you are supposed to think! – Markus Jun 06 '16 at 06:58
  • or you can use a good ol' Stringbuilder and then convert it to your needs – Zeromus Jun 06 '16 at 08:19