If I had the following
str1 = " Just a test "
str2 = " "
l1 = str1.strip().split()
l2 = str2.strip().split()
I'd get
l1 = ["Just", "a", "test"]
l2 = []
How would I accomplish this in Java?
If I had the following
str1 = " Just a test "
str2 = " "
l1 = str1.strip().split()
l2 = str2.strip().split()
I'd get
l1 = ["Just", "a", "test"]
l2 = []
How would I accomplish this in Java?
You could use String.trim()
and String.split(String)
(which takes a regular expression). Something like,
String str1 = " Just a test ";
String str2 = " ";
String[] l1 = str1.trim().split("\\s+");
String[] l2 = str2.trim().split("\\s+");
System.out.println(Arrays.toString(l1));
System.out.println(Arrays.toString(l2));
Outputs (the requested)
[Just, a, test]
[]
You can use Java's trim()
and split("\\s+")
For example
String str1 = " Just a test ";
String[] toks = str1.trim().split("\\s+");
Then toks
will be ["Just", "a", "test"]