Regarding Java String Manipulation

I have the string "MO""RET" , which is stored in the items[1] array after the split command. After it is saved, I replace this line and replace all double quotes. But I want it to be stored as MO"RET . How do I do this. In the csv file from which I process the split command, double quotes are repeated in the contents of the text field (example: this account is ""large"" one). Therefore, I want to keep one of the two quotes in the middle if it repeats and ignores the trailing quotes, if any. How can I do this?

 String items[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); items[1] has "MO""RET" String recordType = items[1].replaceAll("\"",""); 

After that, recordType has MORET I want it to have MO"RET

+2
java string replaceall
source share
4 answers

Do not use regex to separate CSV strings. This requires trouble;) Just analyze it by nature. Here is an example:

 public static List<List<String>> parseCsv(InputStream input, char separator) throws IOException { BufferedReader reader = null; List<List<String>> csv = new ArrayList<List<String>>(); try { reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); for (String record; (record = reader.readLine()) != null;) { boolean quoted = false; StringBuilder fieldBuilder = new StringBuilder(); List<String> fields = new ArrayList<String>(); for (int i = 0; i < record.length(); i++) { char c = record.charAt(i); fieldBuilder.append(c); if (c == '"') { quoted = !quoted; } if ((!quoted && c == separator) || i + 1 == record.length()) { fields.add(fieldBuilder.toString().replaceAll(separator + "$", "") .replaceAll("^\"|\"$", "").replace("\"\"", "\"").trim()); fieldBuilder = new StringBuilder(); } if (c == separator && i + 1 == record.length()) { fields.add(""); } } csv.add(fields); } } finally { if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {} } return csv; } 

Yes, there is a small regular expression, but it only trims the end of the separator and the surrounding quotation marks of one field.

However, you can also grab the third-party Java CSV API .

+6
source share

What about:

 String recordType = items[1].replaceAll( "\"\"", "\"" ); 
+1
source share

I prefer to use replacement instead of replaceAll. replaceAll uses REGEX as the first argument.

The requirement is to replace the two remaining QUOTES with one QUOTE

 String recordType = items[1].replace( "\"\"", "\"" ); 

To see the difference between replace and replaceAll, run the following code

 recordType = items[1].replace( "$$", "$" ); recordType = items[1].replaceAll( "$$", "$" ); 
0
source share

You can use regex here.

 recordType = items[1].replaceAll( "\\B\"", "" ); recordType = recordType.replaceAll( "\"\\B", "" ); 

The first statement replaces the quotation marks at the beginning of the word with an empty character. The second statement replaces the quotation marks at the end of the word with an empty character.

0
source share

All Articles