如何在 Kotlin 中将字符串拆分为数组? [英] How could I split a String into an array in Kotlin?

查看:50
本文介绍了如何在 Kotlin 中将字符串拆分为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将从文件中读取的字符串拆分为一组值.我想在逗号处拆分字符串,例如,如果字符串读取:

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"

数组中的值将是:

  • 数组索引 0 - 名称
  • 数组索引 1 - 2012
  • 数组索引 2 - 2017

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

I found this example in Java:

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

我如何在 Kotlin 中做到这一点?

How I could do it in Kotlin?

推荐答案

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

这篇关于如何在 Kotlin 中将字符串拆分为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆