加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

红宝石 – 如何删除一个尾随的逗号?

发布时间:2020-12-16 20:06:44 所属栏目:百科 来源:网络整理
导读:给出字符串: BobBob,BobBob Burns, 你如何返回那个w / o一个逗号? BobBobBobBob Burns 另外,我想这个方法不会打破,如果通过一个零,只是为了返回一个零? def remove_trailing_comma(str) !str.nil? ? str.replace(",") :nilend 解决方法 我的想法是使用 st
给出字符串:
Bob
Bob,Bob
Bob Burns,

你如何返回那个w / o一个逗号?

Bob
Bob
Bob
Bob Burns

另外,我想这个方法不会打破,如果通过一个零,只是为了返回一个零?

def remove_trailing_comma(str)
  !str.nil? ? str.replace(",") :nil
end

解决方法

我的想法是使用 string.chomp:

Returns a new String with the given record separator removed from the end of str (if present).

这是否做你想要的?

def remove_trailing_comma(str)
    str.nil? ? nil : str.chomp(",")
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读