Skip to content
Advertisement

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 are dealing/splitting with any of the below special characters (used by Regular Expression): OR

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 it keeps the leading and trailing punctuation. Input of ‘Tis the season, for

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 literal comma, zero or

How to split a string with any whitespace chars as delimiters

What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (‘ ‘, ‘t’, ‘n’, etc.) as delimiters? Answer Something in the lines of This groups all white spaces as a delimiter. So if I have the string: This should yield the strings “Hello” and “World” and omit

Advertisement