gpt4 book ai didi

ruby - 按单个空格拆分字符串

转载 作者:数据小太阳 更新时间:2023-10-29 07:47:26 24 4
gpt4 key购买 nike

我有一个字符串:

s = "alpha beta      gamma delta"

我试图用一个空格作为分隔符来分割这个字符串,而不删除额外的空格来得到这个:

["alpha", "beta", "     gamma", "delta"]

有办法吗?

以下没有给出我想要的结果:

s.split(" ") # => ["alpha", "beta", "gamma", "delta"]
s.split # => ["alpha", "beta", "gamma", "delta"]

最佳答案

尝试指定一个单词边界后跟一个空格:

string = "alpha beta      gamma delta"
p string.split(/\b\s/)
# ["alpha", "beta", " gamma", "delta"]

关于ruby - 按单个空格拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615332/

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