To my understanding following program should print 0,0
as an output.
However, when I run this program I am getting 1,0
as an output.
public class Test1 {
public static void main(String[] args) {
System.out.println("".split(";").length); //1
System.out.println(";".split(";").length);//0
}
}
Please help me understand what is going on here ?
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
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.
Split() String method in Java with examples 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.
As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.
relavant code
// If no match was found, return this
if (off == 0)
return new String[]{this};
// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With