I was just wondering what the best way to remove the white space from all the elements of a list would be.
For example if I had String [] array = {" String", "Tom Selleck "," Fish "}
How could I get all the elements as {"String","Tom Selleck","Fish"}
Thanks!
Another java 8 lambda option :
And the ugly but optimized version without new array creation
Original "array" is modified.
Not knowing how the OP happened to have
{" String", "Tom Selleck "," Fish "}
in an array in the first place (6 years ago), I thought I'd share what I ended up with.My array is the result of using split on a string which might have extra spaces around delimiters. My solution was to address this at the point of the split. My code follows. After testing, I put splitWithTrim() in my Utils class of my project. It handles my use case; you might want to consider what sorts of strings and delimiters you might encounter if you decide to use it.
Output of running the test application is: