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"

数组中的值为:

  • array index 0 - name
  • 数组索引1-2012
  • array index 2 - 2017

我在Java中找到了这个例子:

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

How I could do it in Kotlin?

推荐答案

val strs = "name, 2012, 2017".split(",").toTypedArray()

Kotlin相关问答推荐

这些Kotlin函数等效吗?

多模块Kotlin项目中的FreeFair

用vararg替换列表的设计弃用警告

Kotlin接口方法默认值&;可传递依赖项

kotlin - 挂起简单方法调用链时可能存在冗余分支

如何使用multiset与JOOQ获取关联的记录列表?

generic 类实例列表 - 调用采用 T 的函数

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

如何在 Spring Boot 3 中为内部类提供运行时提示

Kotlin 列表扩展功能

在 Kotlin 中,我可以在集合上有一个条件构建器元素吗?

使用空键映射获取

Android 导航组件 - 向上导航打开相同的片段

Spring webflux bean验证不起作用

Kotlin 顶级函数与对象函数

Android Room - error: Cannot figure out how to save this field into database

Kotlin flatMap - map

Kotlin:测试中的 java.lang.NoSuchMethodError

Kotlin反射不可用

Android Jetpack Compose - 图像无法zoom 到框的宽度和高度