1

I have a String str = "13-005B,162-,0000-". I have a function named outputUtill which need a input in format:- ="(string1 value)",="(string2 value)",="(string3 value)". The outputUtill function splits the input string based on "," as delimiter. Is there any way so I can make the commas(,) escapable in String str so that when it becomes of format:- ="13-005B,162-,0000-" and passes to outputUtill function it does not get split into three substring

  1. 13-005B
  2. 162-
  3. 0000-

For example, we pass

="370143030",="SPRING TOWNSHIP CENTRE",="1869 ZION ROAD BELLEFONTE PA 16823",="8143551",="02",="13-005B,162-,0000-", 

to the outputUtill function. We want output as

370143030
SPRING TOWNSHIP CENTRE
1869 ZION ROAD BELLEFONTE PA 16823
8143551
02
13-005B,162-,0000-
Rishikesh-12
  • 43
  • 1
  • 2
  • 7
  • You could replace all the commas with another character or sequence – Adriaan Koster Nov 05 '21 at 10:46
  • Suppose we pass ="370143030",="SPRING TOWNSHIP CENTRE",="1869 ZION ROAD BELLEFONTE PA 16823",="8143551",="02",="13-005B,162-,0000-", to the outputUtill function. We want output as 370143030 SPRING TOWNSHIP CENTRE 1869 ZION ROAD BELLEFONTE PA 16823 8143551 02 13-005B,162-,0000- – Rishikesh-12 Nov 05 '21 at 10:47
  • I don't get what should be the result, this question is very confusing. You have "13-005B,162-,0000-" in input, then you need the same string without commas? and should be one string and not 3 separated strings? – Andrei Tornea Nov 05 '21 at 10:50
  • 2
    That's not a valid String – Adriaan Koster Nov 05 '21 at 10:50
  • check the question once again. I have tried to make it more readable. – Rishikesh-12 Nov 05 '21 at 10:52
  • as @AdriaanKoster said, you can't pass such a string, it's not even a string, try to use lists instead. – Andrei Tornea Nov 05 '21 at 10:54
  • Isn't the entire point of those `="..."` to enclose single values? If so, the problem is not that you need to escape things, it means that you need to fix however you're parsing things right now, because I think it should already work the way you want, and if it doesn't that would be a bug. In other words, your code should not just split on `,`, it needs to actually **parse** the row to distinguish quoted values and separators. – Mark Rotteveel Nov 05 '21 at 10:55
  • Are you in control of implementation of the `outputUtill` function ? Right of the bat the best solution seems to use a different separator. A kind of a separator that you would not encounter within the token body ... Otherwise, you could replace commas with something like "" string in the input before the split, and then replace the "" string back to "," after the split is done. – Arthur Klezovich Nov 05 '21 at 10:55
  • The main input and output is a csv file. The csv input gets processed and then formatted into the required format and passed on to `outputUtill` function. That is a reason for which I cannot replace "," with "" or any other delimiter. – Rishikesh-12 Nov 05 '21 at 11:02
  • Maybe take a look at OpenCSV (https://mkyong.com/java/how-to-read-and-parse-csv-file-in-java/) – Adriaan Koster Nov 05 '21 at 11:09

1 Answers1

0

outputUtil could split on the sequence ",=" and trim the leading and ending =" and ",

public static void main(String[] args) {
    String input = "=\"370143030\",=\"SPRING TOWNSHIP CENTRE\",=\"1869 ZION ROAD BELLEFONTE PA 16823\",=\"8143551\",=\"02\",=\"13-005B,162-,0000-\", ";
    String sep = "\",=\"";

    String[] output = input.substring(2, input.length() - 3).split(sep);
    Arrays.stream(output).forEach(s -> System.out.println(s));
}

Output:

370143030
SPRING TOWNSHIP CENTRE
1869 ZION ROAD BELLEFONTE PA 16823
8143551
02
13-005B,162-,0000-
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60