我有一个已被读取并分成3个不同的csv的csv。 CSV是通过管道分隔的,并且split变量保存在字符串变量中。我想将新字符串拆分为逗号分隔的字符串,但是一旦这样做,它就会出现异常。
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];
["Active Catalog Detail (Network Id "|" Category Ids "] ["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]);
答案 0 :(得分: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
答案 1 :(得分:0)
您要将.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("\\,");