Reputation: 485
I have a line in the .csv file as abc,bcc,
I have to separate it into three tokens abc
, bcc
and null
.
First, I tried StringTokenizer
but it did not return a null
token. Then, I tried String.split(",")
but it also did not return null
at the end; it returned a string which has null
in between but not at the end.
so please help me thanks in advance.
Upvotes: 0
Views: 653
Reputation: 29680
Use the two argument split
method with a negative second argument:
String str = "abc,bcc,";
String[] tokens = str.split(",", -1);
The relevant documentation part:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
- If the limit is positive then the pattern will be applied at most limit - 1 times, the array's length will be no greater than limit, and the array's last entry will contain all input beyond the last matched delimiter.
- If the limit is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
- If the limit is negative then the pattern will be applied as many times as possible and the array can have any length.
Upvotes: 3
Reputation: 308031
Try the String.split()
variant that takes a limit and pass a negative number to it.
Upvotes: 2