Reputation: 27
I have a String, which can have an multiple number of words which are dot separated.
String s = "text.text1.text2.text3";
I want to generate a List which adds a Element for each word in the String.
List<String> list= Arrays.asList(s.split("/./"));
generates just 1 Element.
Is there a fast way to do this?
Upvotes: 1
Views: 4693
Reputation: 1896
Just the token is wrong, here is the correct code:
import java.util.*;
public class SeperateString {
public static void main(String[] args) {
String s = "text.text1.text2.text3";
List<String> list= Arrays.asList(s.split("\\."));
System.out.println(list);
}
}
And here is the output:
[text, text1, text2, text3]
Upvotes: 1
Reputation: 521249
String#split
is the way to go here, except that you want to split on the regex pattern \.
by itself, as Java's regex API does not take delimiters:
String s = "text.text1.text2.text3";
List<String> elements = Arrays.asList(s.split("\\."));
System.out.println(elements); // [text, text1, text2, text3]
Upvotes: 7