Removing replaceAll ("[() .- \"] ")

0

I'm using openJDK1.7 , I'm in need of help.

String[] vetor = {"\"Jui.ce \"", "j-90.0", "Abobr.e-u"};

for(int = 0; i < vetor.length; i++)
   vetor[i].replaceAll("[()-.\"]", "");

The above code among others is not giving a problem but is not working, when I apply to remove one at a time from the right. I wanted to remove it all at once. Where am I going wrong?

    
asked by anonymous 26.01.2017 / 23:24

2 answers

1

The String class in Java is immutable. This means that its value does not change after its creation. When you call the replaceAll(String regex, String replacement) method, a new String is returned with the result and the String original remains unchanged. Try this:

String[] vetor = {"\"Jui.ce \"", "j-90.0", "Abobr.e-u"};
String[] resultado = new String[vetor.length];

for(int i = 0; i < vetor.length; i++)
   resultado[i] = vetor[i].replaceAll("[()-.\"]", "");

for(String s : resultado)
    System.out.println(s);
    
27.01.2017 / 00:03
0

Please try as follows

  • "When I apply to remove one at a time from the right", grab and mount this, as follows:
  • ["value-1" | "value-2" | "value-3"] test and see if it works.
  • Detail this can be a lot slower than removing one by one
  • Put the result there, I could not test it here.

So, no, there is a programming error, not a regular expression analysis.

public class ManipulandoString{
  public static void main(String[] args){
        String[] vetor = {"\"Jui.ce \"", "j-90.0", "Abobr.e-u"};
        String[] result = new String[3];
        for(int i=0; i <vetor.length; i++)
            result[i]=vetor[i].replaceAll("[.|\"|\(|\)|-]", "");
        for(int i = 0; i < vetor.length; i++)
         System.out.println(result[i]);
    }

}

  

See the result, I tried to update the test, I think it's there, if not just copy from here to there.

    
26.01.2017 / 23:31