简体   繁体   English

Java的。 如何删除数组上的空白区域

[英]Java. How to remove white space on array

For example I split a string "+name" by +. 例如,我将字符串“+ name”拆分为+。 I got an white space" " and the "name" in the array(this doesn't happen if my string is "name+"). 我在数组中有一个空格“”和“name”(如果我的字符串是“name +”,则不会发生这种情况)。

t="+name";
String[] temp=t.split("\\+");

the above code produces 上面的代码产生

temp[0]=" "
temp[1]=name

I only wants to get "name" without whitespace.. 我只想得到没有空格的“名字”..

Also if t="name+" then temp[0]=name. 如果t =“name +”,则temp [0] = name。 I'm wondering what is difference between name+ and +name. 我想知道name +和+ name之间有什么区别。 Why do I get different output. 为什么我会得到不同的输出。

simply loop thru the items in array like the one below and remove white space 只需循环遍历数组中的项目,如下图所示,并删除空格

for (int i = 0; i < temp.length; i++){
    temp[i] = if(!temp[i].trim().equals("") || temp[i]!=null)temp[i].trim();
}

The value of the first array item is not a space ( " " ) but an empty string ( "" ) . 第一个数组项的值不是空格( " " )而是空字符串( "" The following snippet demonstrates the behaviour and provides a workaround: I simply strip leading delimiters from the input. 以下代码段演示了该行为并提供了一种解决方法:我只是从输入中删除前导分隔符。 Note, that this should never be used for processing csv files, because a leading delimiter will create an empty column value which is usually wanted. 请注意,这绝不应该用于处理csv文件,因为前导分隔符将创建通常需要的空列值。

    for (String s : "+name".split("\\+")) {
        System.out.printf("'%s'%n", s);
    }

    System.out.println();

    for (String s : "name+".split("\\+")) {
        System.out.printf("'%s'%n", s);
    }

    System.out.println();

    for (String s : "+name".replaceAll("^\\+", "").split("\\+")) {
        System.out.printf("'%s'%n", s);
    }

You get the extra element for "+name" 's case is because of non-empty value "name" after the delimiter. 你得到"+name"案例的额外元素是因为分隔符后面的非空值“name”。

The split() function only "trims" the trailing delimiters that result to empty elements at the end of an array. split()函数仅“修剪”结尾分隔符,这些分隔符会导致数组末尾的空元素。 See JavaSE Manual . 请参阅JavaSE手册

Examples of .split("\\\\+") output: .split("\\\\+")输出的示例:

"+++++"     = { }                // zero length array because all are trailing delimiters
"+name+"    = { "", "name" }     // trailing delimiter removed
"name+++++" = { "name" }         // trailing delimiter removed
"name+"     = { "name" }         // trailing delimiter removed
"++name+"   = { "", "", "name" } // trailing delimiter removed

I would suggest preventing to have those extra delimiters on both ends rather than cleaning up afterwards. 我建议防止在两端都有那些额外的分隔符,而不是事后清理

删除空白区域

str.replaceAll("\\W","").

 String yourString = "name +"; yourString = yourString.replaceAll("\\\\W", ""); yourArray = yourString.split("\\\\+"); 

For a one liner : 对于一个班轮:

String temp[] = t.replaceAll("(^\\++)?(\\+)?(\\+*)?", "$2").split("\\+");

This will replace all multiple plus signs by one, or a plus sign at the start by empty String, and then split on plus signs. 这将用一个替换所有多个加号,或者在开头用空字符串替换加号,然后在加号上拆分。 Which will basically eliminate empty Strings in the result. 这将基本上消除结果中的空字符串。

split(String regex) is equivalent to split(String regex, int limit) with limit = 0. And the documentation of the latter states : split(String regex)相当于split(String regex, int limit) ,limit = 0.后者的文档说明:

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 如果n为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串。

Which is why a '+' at the start works differently than a '+' at the end 这就是为什么'+'在开始工作不同于一个'+'

You might want to give guavas Splitter a try. 您可能想尝试一下guavas Splitter It has a nice fluent api to deal with emptyStrings, trim(), etc. 它有一个很好的流畅的api来处理emptyStrings,trim()等。

@Test
public void test() {
    final String t1 = "+name";
    final String t2 = "name+";

    assertThat(split(t1), hasSize(1));
    assertThat(split(t1).get(0), is("name"));

    assertThat(split(t2), hasSize(1));
    assertThat(split(t2).get(0), is("name"));
}

private List<String> split(final String sequence) {
    final Splitter splitter = Splitter.on("+").omitEmptyStrings().trimResults();
    return Lists.newArrayList(splitter.split(sequence));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM