4

I am new to Kotlin . I want to know how the split function is working .Please see the below example.

Example 1:

var str = "Kotlin TutorialsEPTutorialaSEpKartSEpExamples"
var delimiter1 = "SEP"
var delimiter2 = "ASEP"
var delimiter3 = "ASEPP"
val parts = str.split(delimiter1, ignoreCase = true)
print(parts)

Output is (Working - Understood-All Based on "SEP" string ll be splitted ):

 [Kotlin Tutorial, Tutoriala, Kart, Examples]

Example 2:

var str = "Kotlin TutorialsEPTutorialaSEpKartSEpExamples"
var delimiter1 = "SEP"
var delimiter2 = "ASEP"
var delimiter3 = "ASEPP"
val parts = str.split(delimiter1,delimiter2,ignoreCase = true)
print(parts)

Output is (Working - Understood-All Based on "SEP"and "ASEP" string ll be splitted but I don't know how it is working.After splitting with "SEP" only TutorialA ll come .but how the aSEP is splitting? ):

  [Kotlin Tutorial, Tutorial, Kart, Examples]

Example 3:

var str = "Kotlin TutorialsEPTutorialaSEpKartSEpASEPPExamples"
var delimiter1 = "SEP"
var delimiter2 = "ASEP"
var delimiter3 = "ASEPP"
val parts = str.split(delimiter1,delimiter2,delimiter3,ignoreCase = true)

Output: Based on the Second example, ASEPP should be split right ?

[Kotlin Tutorial, Tutorial, Kart, , PExamples]

See P is coming now. Please explain the logic?

1 Answer 1

7

No, your problem is that CharSequence.split(...) "takes" all your delimeters and using loop just goes through your string and removes characters that are equal delimeter in current iteration(vararg delimeter). If you'd write delimeter3 before delimeter2, then you'd get right(in your opinion) result

val str = "Kotlin TutorialsEPTutorialaSEpKartSEpASEPPExamples"
val delimiter1 = "SEP"
val delimiter2 = "ASEP"
val delimiter3 = "ASEPP"
val parts = str.split(delimiter1, delimiter3, delimiter2, ignoreCase = true)
println(parts)

Output

[Kotlin Tutorial, Tutorial, Kart, , Examples]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @gromyk ,Thanks for ur answer. Pls look into the second example. "using loop just goes through your string and removes characters that are equal delimeter in current iteration" as mentioned by you I ll explain the second example. While replacing with first delimiter "SEP" ,output should be "Kotlin Tutorial Tutoriala Kart Examples" ,Then while looping with "ASEP" it didn't find any "ASEP" string in the output. So final output should "Kotlin Tutorial Tutoriala Kart Examples" this one right ? My doubt is how we got output as "Kotlin Tutorial, Tutorial, Kart, Examples"?a missing Tutoriala
From the official documentation: To avoid ambiguous results when strings in delimiters have characters in common, this method proceeds from the beginning to the end of this string, and matches at each position the first element in delimiters that is equal to a delimiter in this instance at that position. kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/split.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.