Reputation: 575
I have a string String A = "/a/./b/../../c/"
and I want to split based on /.
I am writing this way
String[] strings = A.split("/");
AND I am getting this array data. Why I am getting empty space at [0]th
position. I want data starting from s[1]
position.
Value of s is ====:
Value of s is ====:a
Value of s is ====:.
Value of s is ====:b
Value of s is ====:..
Value of s is ====:..
Value of s is ====:c
Thanks in Advance
Upvotes: 1
Views: 50
Reputation: 79085
@dave has already answered the why part of your question. Answering the following remaining part of your question:
I want data starting from s[1] position.
You can use Arrays.copyOfRange
to get a copy of the array of the required range.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String A = "/a/./b/../../c/";
String[] strings = Arrays.copyOfRange(A.split("/"), 1, A.split("/").length);
System.out.println(Arrays.toString(strings));
}
}
Output:
[a, ., b, .., .., c]
Upvotes: 1
Reputation: 2256
You can filter empty strings out after your split code:
List<String> stringsList = Stream.of(strings)
.filter(s -> s.length() > 0)
.collect(Collectors.toList());
Upvotes: 1
Reputation: 11975
From the Java API:
When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array.
In your case the match to the initial /
means an empty string is the first result. You can think of it as what comes before the separator.
Upvotes: 2