gpt4 book ai didi

string - Scala中如何将字符串拆分为字符

转载 作者:行者123 更新时间:2023-12-03 06:01:16 27 4
gpt4 key购买 nike

例如,有一个字符串 val s = "Test"。如何将其分成t、e、s、t

最佳答案

你需要角色吗?

"Test".toList    // Makes a list of characters
"Test".toArray // Makes an array of characters

你需要字节吗?

"Test".getBytes  // Java provides this

需要字符串吗?

"Test".map(_.toString)    // Vector of strings
"Test".sliding(1).toList // List of strings
"Test".sliding(1).toArray // Array of strings

您需要 UTF-32 代码点吗?好吧,这更难了。

def UTF32point(s: String, idx: Int = 0, found: List[Int] = Nil): List[Int] = {
if (idx >= s.length) found.reverse
else {
val point = s.codePointAt(idx)
UTF32point(s, idx + java.lang.Character.charCount(point), point :: found)
}
}
UTF32point("Test")

关于string - Scala中如何将字符串拆分为字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com