Skip to content

Tag: split

Java .split(“|”) not working

I just ran into the problem that the split method for strings wouldn’t work with character “|” as an argument. It somehow separates each character in the string. Code: Output: Answer Use escape character before | like below: Similar “escape character logic” is required, when you …

Java Regular Expression split keeping contractions

When using split(), what regular expression would allow me to keep all word characters but would also preserve contractions like don’t won’t. Anything with word characters on both sides of the apostrophe but removes any leading or trailing apostraphes such as ’tis or dogs’. I have: but…

Split using a bracket

How can I split a string using [ as the delimiter? if I do I get an error Exception in thread “main” java.util.regex.PatternSyntaxException: Unclosed character class near index 1 [ Any help? Answer The [ is a reserved char in regex, you need to escape it,

How to convert comma-separated String to List?

Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that? Answer Convert comma separated String to List The above code splits the string on a delimiter defined as: zero or more whitespace, a…