在逗号分割字符串 [英] Split string at commas
本文介绍了在逗号分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些Java中的字符串,如下所示:
一个String =AAC格式1Zl亚琛,, DE,5048.850N,00611.483E,189.0米,1 ,,,,亚琛
我想在每一个逗号分割字符串并分配分裂值的数组。但我无法弄清楚如何保持多个逗号,这应该是空的空值。
结果数组应该是这样的:
数组[0] =AAC格式1Zl亚琛
阵列[1] = NULL
阵列[2] =DE
数组[3] =5048.850N
数组[4] =00611.483E
阵列[5] =189.0米
阵列[6] =1
阵列[7] = NULL
数组[8] = NULL
阵列[9] = NULL
数组[10] =亚琛
解决方案
您可以使用的分裂,字符串的方法,用空替换空字符串
进口java.util.Arrays中;公共类RegFun { 公共静态无效的主要(字串[] args){
字符串s =\\AAC格式1Zl亚琛\\,, DE,5048.850N,00611.483E,189.0米,1 ,,,, \\亚琛\\;
的String [] =拆分s.split(); 的for(int i = 0; I< split.length;我++){
如果(拆分[I] .equals()){
拆分[我] = NULL;
}
} 的System.out.println(Arrays.toString(分割));
}
}
输出
[AAC格式1Zl亚琛,空,DE,5048.850N,00611.483E,189.0米,1,NULL,NULL,NULL,亚琛]
I have some strings in Java, which look like the following:
String s = ""Aac 1Zl Aachen",,DE,5048.850N,00611.483E,189.0m,1,,,,"AACHEN""
I wanna split the string at every comma and assign the splitted values to an array. But i couldn't figure out how to keep the empty values between the multiple commas, which should be null.
The resulting array should look like this:
array[0] = ""Aac 1Zl Aachen""
array[1] = null
array[2] = "DE"
array[3] = "5048.850N"
array[4] = "00611.483E"
array[5] = "189.0m"
array[6] = "1"
array[7] = null
array[8] = null
array[9] = null
array[10] = ""AACHEN""
解决方案
You can use split, method of String and replace empty string with null
import java.util.Arrays;
public class RegFun {
public static void main(String[] args) {
String s = "\"Aac 1Zl Aachen\",,DE,5048.850N,00611.483E,189.0m,1,,,,\"AACHEN\"";
String[] split = s.split(",");
for (int i = 0; i < split.length; i++) {
if (split[i].equals("")) {
split[i] = null;
}
}
System.out.println(Arrays.toString(split));
}
}
output
["Aac 1Zl Aachen", null, DE, 5048.850N, 00611.483E, 189.0m, 1, null, null, null, "AACHEN"]
这篇关于在逗号分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文