gpt4 book ai didi

Elixir - 拆分字符串以便它不会在列表中返回空字符串?

转载 作者:行者123 更新时间:2023-12-04 10:06:57 25 4
gpt4 key购买 nike

我在正则表达式上拆分字符串。结果数组包含正则表达式匹配的空字符串。我不要那些。例如。,

iex(1)> String.split("Hello world. How are you?", ~r/\W/)
["Hello", "world", "", "How", "are", "you", ""]

如何拆分字符串,使其不会在列表中返回空字符串?

最佳答案

the String.split docs 中所述,

Empty strings are only removed from the result if the trim option is set to true (default is false).



因此,您需要在调用 String.split 时将其添加为选项。 :
String.split("Hello world. How are you?", ~r/\W/, trim: true)
["Hello", "world", "How", "are", "you"]

关于Elixir - 拆分字符串以便它不会在列表中返回空字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37156270/

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