【问题标题】:How to start replacing characters at some point?如何在某个时候开始替换字符?
【发布时间】:2014-10-27 22:38:21
【问题描述】:

这个问题很难形成,我敢肯定它仍然不清楚。

我有一个 CSV 文件,例如:Firstname;Lastname;Adress;product1;product2;product3;product4;

我想开始替换“;”和 ”::”。问题是,我想在第三个分号之后开始替换。

我知道可以在 while 循环中检查每个字符,当出现分号时,我将计数 +1,如果计数器为 3,我将开始替换。但是有没有办法在没有循环的情况下做到这一点?

【问题讨论】:

    标签: java csv replace


    【解决方案1】:

    正则表达式解决方案怎么样?

    Pattern pattern = Pattern.compile("(.*?;.*?;.*?;)(.*)");
    Matcher match = pattern.matcher(str);
    
    if(match.matches()) {
        String firstThree = match.group(1);
        String rest = match.group(2);
        rest = rest.replace(";", "::");
        return firstThree + rest;
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 indexOf(char,fromIndex) 方法。 您的第三个分号位置搜索可以内联:

      csvLine.indexOf(';', csvLine.indexOf(';', csvLine.indexOf(';') + 1) + 1)
      


      我们假设我们的 csvLine 至少有 3 个分号...

          String csvLine = "Firstname;Lastname;Adress;product1;product2;product3;product4";
      
          //Index of "fromIndex" param is inclusive, that's why we need to add 1
          int pos = csvLine.indexOf(';', csvLine.indexOf(';', csvLine.indexOf(';') + 1) + 1);
      
          //Retrieve string from the char after the third semi-colon
          String truncatedLine = csvLine.substring(pos + 1);
      
          //Replace ";" by "::" on our substring
          truncatedLine = truncatedLine.replaceAll(";", "::");
      
          //Then concat the first part of csvLine with the second
          String result = csvLine.substring(0, pos + 1).concat(truncatedLine); 
      
          System.out.println(result);  //Print => Firstname;Lastname;Adress;product1::product2::product3::product4
      

      输入控制和性能不佳,但我们没有任何循环 :)

      【讨论】:

        【解决方案3】:
        int i = 0;
        int pos = 0;
        while (i < 3) {
            pos = string.indexOf(';', pos+1);
            i++;
        }
        String newString = string.substring(0, pos) +";"+ (string.substring(pos + 1, string.length()).replace(";", "::")); 
        

        【讨论】:

          【解决方案4】:

          如果我明白你想要什么,试试这个。 先搜索第三个分号的se位置:

          String csvContent = "Firstname;Lastname;Adress;product1;product2;product3;product4;";
          int i = 0;
          int index= 0;
          while(i < 4){
            index = csvContent.indexOf(';', (index + 1));
            i++;
          }//index = position of the thrid semicolon
          

          其次,在索引位置剪切你的 CSV 内容。

          String tmp1 = csvContent.substring(0, index);
          String tmp2 = csvContent.substring(index, csvContent.length());
          

          三、全部替换';'通过'::':

          tmp2 = tmp2.replaceAll(";", "::");
          

          最后,重建你的文件内容:

          csvContent = tmp1 + tmp2;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-11-22
            • 2020-08-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多