4

I'm taking my first steps in Kotlin, and trying to write a simple string split function. I started with this:

fun splitCSV(s : String) : Array<String> {
    return s.split(",");
}

Which I guess can be also written like this:

fun splitCSV(s : String) : Array<String> = s.split(",");

But I'm getting a type error, since s.split returns an Array<String?>? and not Array<String>. I couldn't find a simple way to do a cast, so I wrote this function to do the conversion:

fun forceNotNull<T>(a : Array<T?>?) : Array<T> {
    return Array<T>(a!!.size, { i -> a!![i]!! });
}

fun splitCSV(s : String) : Array<String> = forceNotNull(s.split(","));

However, now I'm getting a runtime error:

ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String

If I change T in forceNotNull to String, then it works, so I guess I'm close to a solution.

Is this the right way to go about it? And if it is, how can I fix forceNotNull to work in the generic case?

5
  • 1
    Function sureItemsNotNull already added: youtrack.jetbrains.com/issue/KT-1391 Commented Apr 3, 2012 at 21:21
  • @StasKurilin can you give an example of how that could be used with the above function definition? Commented Apr 4, 2012 at 7:55
  • 2
    The exception you are getting is a bug, of course. The easiest solution in this case is just a cast: s.split(",") as Array<String> Commented Apr 8, 2012 at 15:13
  • @Andery I guess the warning "This cast can never succeed" is also a bug. Nice to hear from the authority, thanks! Commented Apr 9, 2012 at 7:14
  • This should be closed as a duplicate of 33583235. The other question is newer, more direct and answers are for current kotlin 1.0 betas and beyond. Commented Dec 29, 2015 at 5:09

3 Answers 3

1

Not sure it's the best method, but this seems to work:

fun splitCSV(s : String) : Array<String> {
  return ( s.split(",") as? Array<String>? ).sure() ;
}

Although IntelliJ highlights the as? with "This cast can never succeed"... So my initial optimism is fading

Oddly though, it seems to work...

As does:

fun splitCSV(s : String) : Array<String> {
  return s.split(",").sure() as Array<String> ;
}

But with the same warning... I'm getting confused, so I'll stop now :-/

Edit

Of course, you can get it to work with List<String>:

import java.util.List

fun splitCSV(s : String) : List<String> {
  return s.split(",")!!.map<String?,String> { it!! }
}

but that wasn't the question ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

Like Andrey said, the cast is the right approach, so you were very close... Accepting this.
Can you update the code here for .sure() to !! since that is outdated code.
1

Since the time of this post, kotlin has changed a lot and now there are lot easier and nice(explicit casts should be avoided) ways of converting Array<T?>? to Array<T>, one way would be to do

val data: Array<String?>? = getData()
val notNullArray: Array<String> = data?.filterNotNull()?.toTypedArray() ?: arrayOf()

here we make use of filterNotNull to get a List<String> and then convert that list to an Array<String>, if however the original array is null then we simply use arrayOf(), which given us an empty Array<T>

?: is the elvis operator which returns the expression on its left if its not null otherwise it returns the value of expression on the right

Comments

0

in Simple way you can cast string to Array in kotlin by:-

fun splitCSV(s: String) = s.split(",").toTypedArray()

call this function by

val array: Array<String> = splitCSV("ABC,XYZ,test,rupesh")

in kotlin CharSequence.split(regex) return List so if you want to change in type Array use .toTypedArray() method

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.