将字符串拆分为长度为1的单个字符串:如何避免空字符串?

huangapple go评论59阅读模式
英文:

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 a Sequence of characters.
  • asIterable() gives an Iterable of characters. (You can do most things to an Iterable that you can to a List.)
  • toCharArray() gives a CharArray. (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 splitting, 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(&quot;&quot;).filter(String::isNotEmpty).toList()

Alternatively, just remove the first and last splits - those are the only ones that will be empty.

str.split(&quot;&quot;).run { subList(1, lastIndex) }
// note that subList produces a &quot;view&quot; 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(&quot;(?&lt;!^)(?!$)&quot;.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(&quot;&quot;).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 = &quot;Testing 1234&quot;;
val segm = str.split(&quot; &quot;).join(&quot;&quot;)
        
println(segm);
println(segm.length)

huangapple
  • 本文由 发表于 2023年5月22日 12:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定