如何在Java字符串中用一个双引号替换两个双引号?

如何在Java字符串中用一个双引号替换两个双引号?,java,Java,我正在读取一个CSV文件,其中有一些值,如 field 1 field 2 field 3 1 test case1 expecting one, and \"two\", and three 将文件读入StringBuilder并转换toString()后,我将文件内容拆分为:string.split(“,(?=([^\”]*\“[^\”]*\”*[^\“]*$”); 迭代字符串时,我得到以下值:- 1 test case1 expect

我正在读取一个CSV文件,其中有一些值,如

field 1   field 2           field 3
1        test case1         expecting one, and \"two\", and three
将文件读入
StringBuilder
并转换
toString()
后,我将文件内容拆分为:
string.split(“,(?=([^\”]*\“[^\”]*\”*[^\“]*$”);

迭代字符串时,我得到以下值:-

1
test case1
expecting one, and ""two"", and three
我如何用单双引号替换两个双引号,如“2”

这是我的密码:-

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class csvStringParser {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub


          String path = "E:/spc.csv";

            String read = readFile(path);
            System.out.println("content of the file before \" = \n"  +read);



//      System.out.println("content of the file after= \n"  +read);

        String[] tokens = read.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
            for(int i = 0;i<tokens.length;i++) {
                String abc = tokens[i].replace("\"\"", "\"");

           //   if(abc.length()>2){
                if(abc.startsWith("\"") && abc.endsWith("\"")){ 
                abc = abc.substring(1, abc.length()-1);
                }
            //  } 

                System.out.println("> "+abc);
            }

    }

    public static String readFile( String file ) throws IOException {
        BufferedReader reader = new BufferedReader( new FileReader (file));
        String         line = null;
        StringBuilder  stringBuilder = new StringBuilder();
        String         ls = System.getProperty("line.separator");

        while( ( line = reader.readLine() ) != null ) {
            stringBuilder.append( line );
            stringBuilder.append( ls );
        }

        return stringBuilder.toString();
    }

}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
公共类csvStringParser{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
//TODO自动生成的方法存根
字符串path=“E:/spc.csv”;
字符串读取=读取文件(路径);
System.out.println(“读取\“=\n”+之前的文件内容);
//System.out.println(“读取后文件的内容=\n”+read);
String[]tokens=read.split(“,(?=([^\”]*\“[^\”]*\”*[^\“]*\”*”)*[^\“]*$”);
对于(int i=0;i“+abc);
}
}
公共静态字符串读取文件(字符串文件)引发IOException{
BufferedReader reader=新的BufferedReader(新文件读取器(文件));
字符串行=null;
StringBuilder StringBuilder=新的StringBuilder();
字符串ls=System.getProperty(“line.separator”);
而((line=reader.readLine())!=null){
stringBuilder.append(行);
stringBuilder.append(ls);
}
返回stringBuilder.toString();
}
}
使用

String[]tokens=read.split(“,(?=([^\”]*\“[^\”]*\”*[^\“]*$”)”);

对于(int i=0;如果我写String read=readFile(path);read.replace(“\”,“\”);我现在确实看到了它。不,我没有看到任何明显的东西。你能给我一个ideone示例吗?我的代码读取一个csv文件,我在ideone无法提供。但是如果你创建一个csv文件(逗号分隔符)并将三个字段值放在一行中,它可以很好地读取内容,但两个双引号exists@Matt球我得到了问题,有一个不同的字符序列集由csv的阅读后…我必须转换\ \“\”到\“。谢谢
String input = "this string \"\"has\"\" double quotes";
String output = input.replace("\"\"", "\"");
String[] tokens = read.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
for(int i = 0;i<tokens.length;i++)
    tokens[i]=StringEscapeUtils.unescapeCsv(tokens[i]);