0

I'm having a string which is delimited by comma such as:

  1. if the value is an integer then comma integer value comma (ex: ,10,)
  2. if the value is a date/string then comma double cotes string/date double cotes comma (ex: ,"myStringValue",)

The issue is at some point some where i'm expecting to find a string using the above logic i may have anoter comma e.g: comma double cotes string comma string double cotes comma. (ex: ,"myStringValue, withCommaIssue",)

Is there any way i can still split my inital string? Can I use a regex and the logic for it to be comma followed by double (,") cotes and the end of the string to be signaled only by double cotes comma (",)? But for this case will the other substrings which are integers still work?

example:

1,20,20,,,"string to be parsed as string","01-jan-2020",,"string having an issue, maybe two",,,

In the example above I am able to split the string just fine in substring just that my issue is with the element at position 9 out of 12 possible elements ("string having an issue, maybe two"). Also all integer values are not having double cotes and the elements with no value are present as well.

Many thanks,

jhamon
  • 3,603
  • 4
  • 26
  • 37
BogdanM
  • 957
  • 4
  • 15
  • 32
  • 1
    Does this answer your question? [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – jhamon Feb 20 '20 at 10:39
  • 1
    your question would be way clearer with actual examples instead of text – jhamon Feb 20 '20 at 10:40
  • First duplicate was not in Java, see those: [How to handle commas in the data of a csv in Java](https://stackoverflow.com/questions/15979688/how-to-handle-commas-in-the-data-of-a-csv-in-java) and [Splitting a csv file with quotes as text-delimiter using String.split()](https://stackoverflow.com/questions/15738918/splitting-a-csv-file-with-quotes-as-text-delimiter-using-string-split/15905916#15905916) – jhamon Feb 20 '20 at 10:48
  • 1
    Are you working with properly formatted CSV files? Assuming this is a question from real project (not a college exercise or something), consider using one of the libraries that are built for this purpose. Example: https://commons.apache.org/proper/commons-csv/ http://opencsv.sourceforge.net/ – Mark Bramnik Feb 20 '20 at 10:49
  • @MarkBramnik: yes, they are porperly formatted CSV files. The file is already opened and each line is parsed. I am using an Java based ETL tool which has many Java features but also restrictions. I am at the point in which I can parse 90% of the data, but there are cases in which a comma was entered in the string which for me represents someting different from the business perspective. – BogdanM Feb 20 '20 at 10:54

0 Answers0