我有一个输入流,其中的字段由制表符(\ t)分隔 看起来像这样
.ipa
当我执行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
但是在这种情况下,最后一个字段应该为空,但是当我在修剪和分割后使用此字符串时,我的大小为7
String str1 = "acc123\tdpId123\t2011-01-01\t2022-01-01\thello@xyz.com\tIN\t1233\t";
但是我想要
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
如何避免这种情况?
答案 0 :(得分:3)
去那里:
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
答案 1 :(得分:2)
您可以像这样使用split:
An error occurred while communicating with Other Databases (ODBC).
Unable to connect to the server. Check that the server is running and that you have access privileges to the requested database.
FATAL: connection requires a valid client certificate
Generic ODBC requires additional configuration. The driver and DSN (data source name) must be installed and configured to match the connection.
Unable to connect to the server "<AWS EC2 instance>" using the driver "PostgreSQL Unicode". Check that the server is running and that you have access privileges to the requested database.
为避免空格,可以使用
String[] split = str.split("\t", -1); // note the -1
答案 2 :(得分:1)
您可以使用limit参数来解决此str.split("\t",-1)
。
limit参数控制应用图案的次数,因此会影响结果数组的长度。
在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);
}
}
答案 3 :(得分:1)
在您的情况下,此方法在概念上正确的方法是先分割,然后才修剪第一个和最后一个元素:
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格式,这对于这些事情要好得多。
答案 4 :(得分:0)
尝试一下(结果数组在变量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));