How do I remove spaces (not all spaces, just the beginning and end) of all the indexes of a String array in Java?
How do I remove spaces (not all spaces, just the beginning and end) of all the indexes of a String array in Java?
Using trim()
:
String[] strArray = {" teste ", " teste ", " teste "};
for(int i = 0; i < strArray.length; i++){
strArray[i] = strArray[i].trim();
}
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);