英文:
Split string into single strings of length 1: How to avoid empty string?
问题
The translated code snippet is:
val segm = str.split("")
println(segm)
And the translated question is:
如何使用String.split()以避免在结果数组中出现空字符串?
英文:
val segm = str.split("")
// .filter { it -> it.length > 0 }
println(segm)
With "Testing 1234" as "str" I get "[, T, e, s, t, i, n, g, , 1, 2, 3, 4, ]"
With the additional filter it works. But nevertheless: How do I have to use String.split() in a way, that I avoid empty strings in the result-array?
答案1
得分: 2
Frame challenge: 你正在将字符串拆分为单个字符,而有几种现有的方法可以将字符串拆分为字符,例如:
asSequence()
会给出一个字符序列Sequence
。asIterable()
会给出一个字符可迭代对象Iterable
。(你可以对 Iterable 做大多数 List 可做的操作。)toCharArray()
会给出一个字符数组CharArray
。(尽管在 Kotlin 中通常更喜欢可迭代对象,因为它们更灵活且得到更好的支持。)forEach()
对每个字符执行给定的操作。(这意味着你可以这样做:for (c in myString) { … }
。)
使用其中之一会比使用 split
更简单,可能更高效;这也会使代码的意图更容易理解给任何阅读它的人。
英文:
Frame challenge: what you're doing is splitting the string into individual characters — and there are several existing methods which can do that to a string, such as:
asSequence()
gives aSequence
of characters.asIterable()
gives anIterable
of characters. (You can do most things to an Iterable that you can to a List.)toCharArray()
gives aCharArray
. (Though iterables are generally preferred in Kotlin as they're more flexible and better-supported.)forEach()
performs a given action on each character. (This means you can do e.g.for (c in myString) { … }
.)
Using one of these will be simpler than split
ting, and probably more efficient; it'll also make the intent of the code easier to anyone reading it.
答案2
得分: 1
没有这样的内置API可以忽略空分割,并且坦白说,我不明白为什么不能使用 filter
的问题。
如果你不喜欢 filter
无谓地遍历分割项,你可以使用 splitToSequence
,它会产生一个惰性序列。
str.splitToSequence("").filter(String::isNotEmpty).toList()
或者,你也可以直接移除第一个和最后一个分割项 - 这两个是唯一可能为空的。
str.split("").run { subList(1, lastIndex) }
// 注意,subList 会产生原列表的“视图”,而不是创建一个副本
如果你真的只想使用 split
,你可以使用接受 Regex
参数的重载版本,并使用一个匹配字符串中除了开头和结尾以外所有零宽位置的正则表达式。
// 这会匹配字符串中除了开头(^)和结尾($)以外的任何零宽位置
str.split("(?<!^)(?!$)".toRegex())
这种方法可能比其他方式慢得多,因为使用正则表达式进行匹配相对复杂。
英文:
There isn't such a built-in API that ignores empty splits, and frankly, I don't see what's wrong with using a filter
.
If you don't like the fact that filter
loops over the splits unnecessarily, you can splitToSequence
, which produces a lazy sequence.
str.splitToSequence("").filter(String::isNotEmpty).toList()
Alternatively, just remove the first and last splits - those are the only ones that will be empty.
str.split("").run { subList(1, lastIndex) }
// note that subList produces a "view" of the original list
// it does not create a copy
If you really want to only use split
, you can use the overload that takes a Regex
and use a regex that matches all zero-width positions in the string, except the start and end.
// this matches any position where there is no start of string (^) before it, and no end of string ($) after it
str.split("(?<!^)(?!$)".toRegex())
This is probably going to be a lot slower than the other approaches, since matching against regex is complicated.
答案3
得分: 1
str.split("").filterNot { it.isBlank() }
可以翻译为:str.split("").filterNot { it.isBlank() }
英文:
str.split("").filterNot { it.isBlank() }
答案4
得分: 0
你可以使用空格分割字符串,然后连接字符串,然后将结果字符串推入数组。它将正常工作。
英文:
You can split the string with space and join the string and then you can push the resulted string into array.It will work just fine.
val str = "Testing 1234";
val segm = str.split(" ").join("")
println(segm);
println(segm.length)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论