-2

I have been search the net for a suitable code for this requirement, but not getting any.

I have this csv file (values are not exact, one of field of the actual csv contains bmp image code)

"NAME","ROLENUMBER","SECTION","SEAT","COMMENT" "Abhik","45","AVC","A112004DDG","HELLO3434?" "Bob",,"AVC","FDFDF1212","IN THIS FIELD THE USER CAN MISTAKENLY ADD TWO COMMAS LIKE ,, OR ,,," "JOHN",,,"DFDFEDD22E","HEL;O AGAIN"

the requirement is to somehow insert a "" between every two or more ,(commas) (,, or more commas will tell me that the field value is blank except for the comment field where the user can enter that by mistake as shown in the example) so that program can understand the blank values (it is an existing logic which I dont want to change very much if Ican help it)

So after parsing the csv rows should look like "NAME","ROLENUMBER","SECTION","SEAT","COMMENT" "Abhik","45","AVC","A112004DDG","HELLO3434?" "Bob","","AVC","FDFDF1212","IN THIS FIELD THE USER CAN MISTAKENLY ADD TWO COMMAS LIKE ,, OR ,,," "JOHN","","","DFDFEDD22E","HEL;O AGAIN"

Please help! Thanks in advance!

4

3 回答 3

0

如果您正在使用 CSV 文件,请使用 Apache CSV 库......它们将帮助您摆脱两个逗号之间的所有引号和空值。

这里的问题是,你不能使用 String 的 replace 或 replaceAll 因为这些逗号也可以是引号内的值的一部分......

于 2013-04-05T14:21:37.573 回答
0
String s = "achintya,jha,,kumar";
String newStr="";
for(int i = 0 ; i < s.length(); i++){
   if(s.charAt(i)==',' && s.charAt(i+1)==','){
        newStr =newStr + s.charAt(i) + "\"\"" + s.charAt(i+1);
        i=i+2;
   }
   newStr = newStr + s.charAt(i);
}
于 2013-04-05T14:24:39.300 回答
0

我认为你需要一个正则表达式。用“,”替换[任何一个字符不是语音标记],[任何一个字符不是语音标记]?

于 2013-04-05T14:25:05.643 回答