Remove spaces from an array of String

2

How do I remove spaces (not all spaces, just the beginning and end) of all the indexes of a String array in Java?

    
asked by anonymous 21.08.2017 / 20:53

2 answers

5

Using trim() :

String[]  strArray = {" teste ", " teste ", " teste "};

for(int i = 0; i < strArray.length; i++){
  strArray[i]  = strArray[i].trim();
}
    
21.08.2017 / 20:55
3

no java-8 you can simply do it this way:

String[] resultado = Arrays.stream(meuArray).map(String::trim).toArray(String[]::new);

Or you can make this version more "dirty" without creating a final array, the last array will be modified automatically

Arrays.stream(meuArray).map(String::trim).toArray(unused -> meuArray);
    
21.08.2017 / 20:56