不要 trim tab(\t) from start/end of a String in JAVA

Don't trim tab(\t) from start/end of a String in JAVA

我有一个输入流,其中包含由制表符 (\t) 分隔的字段 看起来像这样

String str = "  acc123\tdpId123\t2011-01-01\t2022-01-01\thello@xyz.com\tIN\t1233\tSOMETHING      ";

当我做 str = str.trim();

时效果很好
strArray = str.split("\t", -1); 
strArray=["acc123","dpId123","2011-01-01","2022-01-01","hello@xyz.com","IN","1233","SOMETHING"] will give size as 8 

但输入记录中的最后一个字段不是必填项,可以跳过。

所以输入也可以像这样。

 String str1 = "acc123\tdpId123\t2011-01-01\t2022-01-01\thello@xyz.com\tIN\t1233\t";

但在这种情况下,最后一个字段应该为空但是当我在 trim 之后使用此字符串并拆分时,我的大小是 7

str1 = str1.trim();      
strArray = str1.split("\t", -1); 
      strArray=["acc123","dpId123","2011-01-01","2022-01-01","hello@xyz.com","IN","1233"]will give size as 7

但是我想要

strArray=["acc123","dpId123","2011-01-01","2022-01-01","hello@xyz.com","IN","1233",""]

如何避免这种情况?

您可以像这样使用拆分:

String[] split = str.split("\t", -1); // note the -1

要避免空格,您可以使用

 Arrays.stream(split).map(String::trim).toArray(String[]:new);

你可以使用限制参数来解决这个问题str.split("\t",-1)

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

阅读 docs 中有关拆分限制的更多信息。

示例:

public class GFG { 
    public static void main(String args[]) 
    { 
        String str = "a\tb\tc\t"; 
        String[] arrOfStr = str.split("\t",-1); 

        for (String a : arrOfStr) 
            System.out.println(a);
        System.out.println(arrOfStr.length);
    } 

} 

试试这个(结果数组在变量 resultArray 中):

    String str1 = "acc123\tdpId123\t2011-01-01\t2022-01-01\thello@xyz.com\tIN\t1233\t";
    String[] strArray = str1.split("\t");
    String regex = ".*\t$";
    String[] resultArray;
    if (str1.matches(regex)) {
        resultArray = new String[strArray.length + 1];
        resultArray[strArray.length] = "";
    } else {
        resultArray = new String[strArray.length];
    }
    for (int i= 0; i < strArray.length; i++) {
        resultArray[i] = strArray[i];
    }

    System.out.println(resultArray.length);
    System.out.println(Arrays.toString(resultArray));

在您的情况下,概念上正确的方法是 先拆分 ,然后才 trim 第一个和最后一个元素:

String[] array = str.split("\t");
array[0] = array[0].trim();
int last = array.length -1;
if (last > 0) {
    array[last] = array[last].trim();
}

此外,如果您预先知道应该有多少个字段,那么您也应该使用该知识,否则您仍然会得到无效的字段数:

int fieldsCount = getExpectedFieldsCount();
String[] array = str.split("\t", fieldsCount);

最后,我建议您不要使用空格作为数据分隔符。用别的东西。比如看CSV格式,这些东西就好多了。

好了:

String str1 = "   acc123\tdpId 123\t201 1-01-01\t2022-01-01\thello@xyz.com\tIN\t1233\t";
str1 = str1.replaceAll("^[ ]+", ""); // removing leading spaces
str1 = str1.replaceAll("[ ]+$", ""); // removing trailing spaces
String[] split = str1.split("\t", -1);

System.out.println(Arrays.toString(split));
System.out.println(split.length);

String#trim 方法也删除了 \t。为了处理这个问题,我使用正则表达式仅删除了前导和尾随空格。

输出:

[acc123, dpId 123, 201 1-01-01, 2022-01-01, hello@xyz.com, IN, 1233, ]
8