在 Java 中拆分已经拆分的数组字符串



我有一个 csv,它已被读取并拆分为 3 个不同的 csv。csv 是管道分离的,拆分变量保存在字符串变量中。我想将新字符串拆分为逗号分隔的字符串,但一旦我这样做,它就会出现异常。

try(BufferedReader br1 = new BufferedReader(new FileReader(newcsvCategory))){
String line;
while ((line = br1.readLine()) != null) {
String[] value1 = line.split("\|,",-1);
String Id = value1[0];
String CatId=value1[1];
["活动目录详细信息(网络 ID "|"类别 ID "] ["209"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["174"|"4900,10082,10119,10358,10039,5132,10011"] ["200"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"] ["181"|"4900,10358,10011"] ["240"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["206"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"] ["255"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["251"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["231"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["179"|"4900,10368,11618,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"] ["184"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"] ["187"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"] ["247"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["215"|"10358"] ["216"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["238"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"] ["224"|">

4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]我想拆分第一列和第二列作为管道分隔,然后进一步将第二列分开为逗号分隔。

我将不胜感激,因为我是新手。

添加了拆分 CatId 的代码:

String[] temp = CatId.split(",",-1);
System.out.println(temp[1]);

真的,无法意识到这些问题,但要做一些笔记。

// this source string: serveral columsn with different separators
String str = "209|4900,10368,11093,11581";

根据您的代码,您尝试将所有单独的数字放入字符串数组中,只需两个步骤:

String[] arr = str.split("\|");    // not line.split("\|,",-1)
// arr[0] = 209
// arr[1] = [4900,10368,11093,11581]
String[] tmp = arr[1].split(",")
// tmp[0] = 4900
// tmp[1] = 10368
// tmp[2] = 11093
// tmp[3] = 11581

如果是这样,您可以通过一个步骤完成:

String[] arr = str.split("[\|,]");
// arr[0] = 209
// arr[1] = 4900
// arr[2] = 10368
// arr[3] = 11093
// arr[4] = 11581

您希望将.split(..( 的限制设置为 2。

while ((line = br1.readLine()) != null) {
String[] value1 = line.split("\|",2);
String Id = value1[0];
String CatId=value1[1]
};

要进一步拆分"CatId"的使用:

// if you need to replace unwanted chars first, you could just use the simple .replace:
CatId = CatId.replace(""", "").replace("[", "").replace("]", "");
// Then, split the array just by ,
String[] catIdArray = CatId.split("\,");

最新更新