94

I need to split a String read in from a file into an array of values. I want to split the String at the commas, so for example, if the String read:

"name, 2012, 2017"

The values in the array would be:

  • array index 0 - name
  • array index 1 - 2012
  • array index 2 - 2017

I found this example in Java:

String[] stringArray = string.split(",");

How I could do it in Kotlin?

1
  • 1
    Just so you know if you have Java code and a Kotlin file open (in IntelliJ/AndroidStudio) and paste it IntelliJ will offer you to auto convert that code to Kotlin. Commented Sep 4, 2017 at 14:44

6 Answers 6

155
val strs = "name, 2012, 2017".split(",").toTypedArray()
Sign up to request clarification or add additional context in comments.

3 Comments

This answer isn't correct. Kotlin's split method will return a List not an array.
In which case you can write: val strs = "name, 2012, 2017".split(",").toTypedArray()
Java's split method actually uses an ArrayList internally so the end result is the same
21

Simple as it is:

val string: String = "leo_Ana_John"
val yourArray: List<String> = string.split("_")

you get: yourArray[0] == leo, yourArray[1] == Ana, yourArray[2]==John

In this case, just change the "_" from my code to ", " of yours. Se bellow

    val yourArray: List<String> = string.split(", ")

Comments

17

If we have a string of values that splited by any character like ",":

 val values = "Name1 ,Name2, Name3" // Read List from somewhere
 val lstValues: List<String> = values.split(",").map { it -> it.trim() }
 lstValues.forEach { it ->
                Log.i("Values", "value=$it")
                //Do Something
            }

It's better to use trim() to delete spaces around strings if exist. Consider that if have a "," at the end of string it makes one null item, so can check it with this code before split :

 if ( values.endsWith(",") )
     values = values.substring(0, values.length - 1)

if you want to convert list to Array ,use this code:

      var  arr = lstValues.toTypedArray()
      arr.forEach {  Log.i("ArrayItem", " Array item=" + it ) }

Comments

7
var newStrg= "853 kB"
val mString = newStrg!!.split(" ").toTypedArray()

Here Split parameter is space

mString[0] = "853"
mString[1] = "kB"

1 Comment

!! in newStrg!! is unnecessary.
2

Split a string using inbuilt split method then using method extensions isNum() to return numeric or not.

fun String.isNum(): Boolean{
   var num:Int? = this.trim().toIntOrNull()
   return if (num != null) true else false
}

for (x in "name, 2012, 2017".split(",")) {
   println(x.isNum())
}

Comments

1

If you want to use multiple/several delimiters in kotlin split, you need to pass them separately:

 val validUrl = "http://test.com/</a> -".split(">", " ", "<").first()

Comments

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.