-2

I am trying to remove all non numeric characters from the String Array as require to compare it with the list of numbers. The split works but I am not able to compare the two Sets

for(String w:a1)
    {
        w=w.replaceAll("[^\\d.]", "");

        if(dContacts.getNumber().equals(w))
        {
            System.out.println("Compared1234567");
        }

        System.out.println("---6545678909876789876hijkhijkhijkjh"+dContacts.getNumber());
        System.out.println("Arraylistextract"+w);
    }
ananymous59
  • 200
  • 1
  • 14

3 Answers3

1

In Java, Strings are immutable. This means that w.replaceAll("[^\\d.]", ""); will create a different String, and w will remain the same. Assign that expression to w to store it:

w = w.replaceAll("[^\\d.]", "");
August
  • 12,410
  • 3
  • 35
  • 51
0

You cannot modify a string without reassigning it to either the same variable or a new one.

abhati
  • 309
  • 1
  • 6
0

String is Immutable so if you are calling some function then it will not change in same object.

you have to change this line to

w= w.replaceAll("[^\\d.]", "");
Prashant
  • 2,556
  • 2
  • 20
  • 26