Scala String split(String regex, int limit) method with example
Last Updated :
29 Oct, 2019
The split(String regex, int limit) method is same as split(String, regex) method but the only difference here is that you can limit the number of elements in the resultant Array.
Method Definition: String[] split(String regex, int limit)
Return Type: It returns a String array where, the number of elements in the Array are specified and the last element of the array is the part that is left in the stated string.
Example: 1#
object GfG
{
def main(args : Array[String])
{
val result = "PfsoQmsoRcsoGfGkso" .split( ".so" , 3 )
for ( m 1 < -result )
{
println(m 1 )
}
}
}
|
So, here we have three elements in the resultant Array and the last element contains the remaining part of the string which is left after splitting the second element of an Array.
Example: 2#
object GfG
{
def main(args : Array[String])
{
val result = "NidhifsoSinghmsoAcso" .split( ".so" , 2 )
for ( m 1 < -result )
{
println(m 1 )
}
}
}
|
Output:
Nidhi
SinghmsoAcso
Share your thoughts in the comments
Please Login to comment...