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?

4
  • What have you tried so far? Commented Jul 17, 2020 at 4: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. Commented Jul 17, 2020 at 4:35
  • String fileContent = "\"abc\",\"bkc\", \"New, York\", \"NY\",\"cool guy\"";<br> String replacorPattern = "[^(\\\)]\\\" *, *\\\"";<br> String result = fileContent.replaceAll(replacorPattern, "\";\"");<br> System.out.println(result);<br> Commented Jul 17, 2020 at 4:47
  • 1
    yes Abra i want to do same Commented Jul 17, 2020 at 6:52

1 Answer 1

-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?

Sign up to request clarification or add additional context in comments.

1 Comment

replace comma with; that don't have a comma inside double quotes - how does your question address this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.