【问题标题】:split an already split array string in Java在Java中拆分已经拆分的数组字符串
【发布时间】:2018-10-24 12:23:38
【问题描述】:

我有一个 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,23011,11132, 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,71232,5233,” ] [“181”|“4900,10358,10011”] ["240"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,23011,11132, 10792"] ["206"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,109132,5233,” ] ["255"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,1132,10011, 5233,10792"] ["251"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011, 5233,10792"] ["231"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,23011,11132, 10792"] ["179"|"4900,10368,11618,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,23011,11132, 10792"] ["184"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,109132,5233,” ] [“187”|“4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,109132,5233,” ] ["247"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011, 5233,10792"] [“215”|“10358”] ["216"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,23011,11132, 10792"] ["238"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,23011,11132, 10792"] ["224"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,23011,11132, 10792"]

我想将第一列和第二列拆分为管道分隔,然后将第二列进一步分隔为逗号分隔。

如果我是新手,我将不胜感激。

添加了拆分 CatId 的代码:

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

【问题讨论】:

  • 您能否通过editing您的问题在您的问题中包含例外情况?

标签: java arrays split


【解决方案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

【讨论】:

    【解决方案2】:

    您想将 .split(..) 的 Limit 设置为 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("\\,");
    

    【讨论】:

    • 好的,不知何故我得到了 value1[1] 中的值。我想在 value1[1] 上的“,”上应用拆分 basesd,但它给出了例外。 java.lang.ArrayIndexOutOfBoundsException: 1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2012-03-21
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多