I have a collection of strings. I need to be able to join the items in this collection into one string and afterwards split that string backwards and get original string collection. Definitely I need to introduce a delimiter character for join/split operation. Given the fact that original strings can contain any characters, I also need to deal with delimiter escaping. My question is very simple - is there a Java class/library that can provide me required functionality out-of-the-box? Something like:
String join(String[] source, String delimiter, String escape);
String[] split(String source, String delimiter, String escape);
or similar, without having to do the work manually?
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array. Input String: 016-78967 Regular Expression: - Output : {"016", "78967"}
The splitlines() method splits a string into a list. The splitting is done at line breaks.
You can split a string by each character using an empty string('') as the splitter. In the example below, we split the same message using an empty string. The result of the split will be an array containing all the characters in the message string.
Without the escaping part, there are:
StringUtils.split(..)
and StringUtils.join(..)
from commons-lang
Joiner
and Splitter
from guava.Splitting: String.split
takes regex pattern as argument (delimeter) and returns String[]
as result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With