Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split & join

Tags:

java

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?

like image 922
andrew.z Avatar asked Feb 03 '12 14:02

andrew.z


People also ask

What is string split?

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.

What is string split in Java?

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"}

How do I split a line in a string?

The splitlines() method splits a string into a list. The splitting is done at line breaks.

How do I split a string into multiple parts?

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.


2 Answers

Without the escaping part, there are:

  • StringUtils.split(..) and StringUtils.join(..) from commons-lang
  • Joiner and Splitter from guava.
like image 158
Bozho Avatar answered Oct 22 '22 15:10

Bozho


Splitting: String.split takes regex pattern as argument (delimeter) and returns String[] as result.

like image 44
Mersenne Avatar answered Oct 22 '22 15:10

Mersenne