Kotlin – Split String

Kotlin Split String using a given set of delimiters or Regular Expression – Splitting a string to parts by delimiters is useful when the string contains many (parametric) values separated by delimiters or if the string resembles a regular expression. In this tutorial we shall learn how to split a string in Kotlin using a given set of delimiters or Regular Expression.

*Delimiter is a character or another string that joins multiple strings into a single one.

Kotlin – Split String using Delimiter(s)

Example – Split String using single Delimiter

In the following example, we shall split the stringKotlin TutorialsepTutorial KartsepExamples  with the Delimiter  sep .

Example – Split String using multiple Delimiters

Multiple delimiters could be provided as arguments to the split() method of String Class.

String. split(delimiter1, delimiter2, .., delimiterN)

In the following example, we shall split the stringKotlin TutorialsepTutorialasepKartsepExamples  with two delimiters  sep ,asep .

Example – Split String ignoring Case

split() method accepts a boolean value after delimiters, whether to ignore the case of delimiters and the string while splitting.

String. split(vararg delimiters, ignoreCase:Boolean = false)

The default argument for ignoreCase is false. To ignore the case, true has to be provided for the ignoreCase as named argument.

In the following example, we shall split the stringKotlin TutorialsEPTutorialaSEpKartSEpExamples  with two delimiters  SEP ,ASEP .

Kotlin – Split String using Regular Expression

Example – Split String using Regular Expression

In the following example, we shall split the stringKotlin TutorialsepTutorialasepKartsepExamples  with the Regular Expression  sep|asep .

Conclusion

In this Kotlin TutorialKotlin Split String, we have learnt to split string using delimiters, ignoring case, and Regular Expression with examples.