0

I have a txt file and it has lines like below:

"abc","bkc", "New York", "NY","cool guy"

I am looking to replace comma with semicolon(;) that don't have a comma inside double quotes:

Desired Output:

"abc";"bkc"; "New York"; "NY";"cool guy"

Is there a way to do this in Java?

Manoj Kumar Dhakad
  • 1,862
  • 1
  • 12
  • 26
  • What have you tried so far? – Scary Wombat Jul 17 '20 at 04:28
  • So you want to replace every occurrence of `","` with `";"`, correct? I mean every appearance of three characters where first character is a double quote, second character is a comma and third character is another double quote. – Abra Jul 17 '20 at 04:35
  • String fileContent = "\"abc\",\"bkc\", \"New, York\", \"NY\",\"cool guy\"";
    String replacorPattern = "[^(\\\\)]\\\" *, *\\\"";
    String result = fileContent.replaceAll(replacorPattern, "\";\"");
    System.out.println(result);
    –  Jul 17 '20 at 04:47
  • 1
    yes Abra i want to do same – Supriya Jul 17 '20 at 06:52

1 Answers1

-1

You can use the .toCharArray() method on that string to generate a array and then iterate trough it.

char[] chars = string.toCharArray();
for(int i = 0; i<chars.length-1; i++){
    if(chars[i]==',') chars[i] = ';'
}

Or is the problem with reading the file?

Hannes
  • 93
  • 5