My question is why the following program:
// Java program to demonstrate working of split(regex,
// limit) with high limit.
public class GFG
{
public static void main(String args[])
{
String str = "geekss@for@geekss";
String [] arrOfStr = str.split("s", 5);
}
}
splits the string "geekss@for@geekss"
into 5 substrings:{"geek", "", "@for@geek", "", ""}
. According to me there should be 4 substrings:{"geek", "","@for@geek", ""}
. Can someone clarify my doubt?
The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.
The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
Whenever there is a need to break bigger strings or a line into several small strings, you need to use the split() function in Python. The split() function still works if the separator is not specified by considering white spaces, as the separator to separate the given string or given line.
Split function returns a list of strings after dividing the string based on the given separator. Following are the advantages of using a split function in python: At some point we may have to break down a large string into smaller chunks or strings. It is the opposite of concatenation, which adds two strings together.
If you look carefully at the docs:
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.
So your resulting array contains two things:
s
(italic part)The reason you got {"geek", "", "@for@geek", ""}
for the first four elements is because they are followed by s
. The last ""
you got is what is left after matching every s
.
Note that the limit
argument of 5
you passed is also related. As per the docs:
If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
So the last matched delimiter is the s
at the very end. After that there is still an empty string that it haven't checked.
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