如何从数组中的所有元素修剪空白? [英] How to trim white space from all elements in array?
本文介绍了如何从数组中的所有元素修剪空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道从列表的所有元素中移除空格的最佳方法是什么。
例如,如果我有 String [] array = {String,Tom Selleck,Fish}
我怎么能得到所有元素 {String ,Tom Selleck,Fish}
谢谢!
试试这个:
String [] trimmedArray = new String [array.length];
for(int i = 0; i< array.length; i ++)
trimmedArray [i] = array [i] .trim();
现在 trimmedArray
包含与 array
,但没有前导和尾随空格。或者,你可以写这个来在同一个数组中就地修改字符串:
for(int i = 0; i < array.length; i ++)
array [i] = array [i] .trim();
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!
解决方案
Try this:
String[] trimmedArray = new String[array.length];
for (int i = 0; i < array.length; i++)
trimmedArray[i] = array[i].trim();
Now trimmedArray
contains the same strings as array
, but without leading and trailing whitespace. Alternatively, you could write this for modifying the strings in-place in the same array:
for (int i = 0; i < array.length; i++)
array[i] = array[i].trim();
这篇关于如何从数组中的所有元素修剪空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文