Multiple delimiters in Scanner class of Java
How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\n) as delimiters? I am parsing some text from a csv file.
How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\n) as delimiters? I am parsing some text from a csv file.
The scanner can also use delimiters other than whitespace. Easy example from Scanner API: The point is to understand the regular expressions (regex) inside the Scanner::useDelimiter. Find an useDelimiter tutorial here. To start with regular expressions here you can find a nice tutorial. Notes
You can use String.Join. If you have a List<string> then you can call ToArray first: In .NET 4 you don’t need the ToArray anymore, since there is an overload of String.Join that takes an IEnumerable<string>. Results: John, Anna, Monica
Single quotes are escaped by doubling them up, just as you’ve shown us in your example. The following SQL illustrates this functionality. I tested it on SQL Server 2008: Results
To do this using re.split you can do: Explaining this briefly: You split on one or more occurrences of the different delimiters (>, !, ]). In order to include the delimiters in the result, you put the pattern in a capturing group by putting parens around it.
Single quotes are escaped by doubling them up, just as you’ve shown us in your example. The following SQL illustrates this functionality. I tested it on SQL Server 2008: Results
Luckily, Python has this built-in 🙂 Update:Following your comment: