代码之家  ›  专栏  ›  技术社区  ›  Gishu

在Ruby中修剪字符串而不创建新字符串的规范方法是什么?

  •  170
  • Gishu  · 技术社区  · 16 年前

    这就是我现在所拥有的——对于它正在做的工作来说,这看起来太冗长了。

    @title        = tokens[Title].strip! || tokens[Title] if !tokens[Title].nil?
    

    假设令牌是通过拆分csv行获得的数组。 现在的功能就像脱衣舞!哎呀!et.如果字符串未被修改,则all返回nil

    "abc".strip!    # => nil
    " abc ".strip!  # => "abc"
    

    如果包含额外的前导或尾随空格而不创建副本,Ruby怎么说呢?

    如果我想变丑 tokens[Title].chomp!.strip!

    9 回复  |  直到 9 年前
        1
  •  248
  •   sameers    10 年前

    我猜你想要的是:

    @title = tokens[Title]
    @title.strip!
    

    这个 #strip! 方法将返回 nil 如果它没有删除任何内容,那么变量本身也会被删除。

    根据Ruby标准,后缀为感叹号的方法可以就地更改变量。

    希望这有帮助。

    更新: 这是输出自 irb 证明:

    >> @title = "abc"
    => "abc"
    >> @title.strip!
    => nil
    >> @title
    => "abc"
    >> @title = " abc "
    => " abc "
    >> @title.strip!
    => "abc"
    >> @title
    => "abc"
    
        2
  •  48
  •   gaRex    9 年前

    顺便说一句,现在Ruby已经支持不带“!”.

    比较:

    p "abc".strip! == " abc ".strip!  # false, because "abc".strip! will return nil
    p "abc".strip == " abc ".strip    # true
    

    也不可能 strip 无重复。请参阅string.c中的源:

    static VALUE
    rb_str_strip(VALUE str)
    {
        str = rb_str_dup(str);
        rb_str_strip_bang(str);
        return str;
    }
    

    Ruby 1.9.3P0(2011年10月30日)【i386-mingw32】

    更新1: 正如我现在看到的——它是在1999年创建的(见 rev #372 在SN):

    更新2: strip! 不会在1.9.x、2.x和主干版本中创建重复的_。

        3
  •  8
  •   user110564    16 年前

    不需要同时删除条带和chomp,因为条带还将删除尾随的回车-除非您更改了默认的记录分隔符,而这正是您要选择的内容。

    奥利的答案已经有了在Ruby中实现这一点的标准方法,但是如果你发现自己经常这样做,你可以为它定义一个方法:

    def strip_or_self!(str)
      str.strip! || str
    end
    

    给:

    @title = strip_or_self!(tokens[Title]) if tokens[Title]
    

    还要记住,if语句将阻止 @title 如果令牌为零,则不会被分配,这将导致它保持其以前的值。如果你愿意或不介意 @标题 始终分配您可以将支票移动到方法中并进一步减少重复:

    def strip_or_self!(str)
      str.strip! || str if str
    end
    

    另一种选择是,如果你觉得冒险,你可以在字符串本身上定义一个方法:

    class String
      def strip_or_self!
        strip! || self
      end
    end
    

    给出其中之一:

    @title = tokens[Title].strip_or_self! if tokens[Title]
    
    @title = tokens[Title] && tokens[Title].strip_or_self!
    
        4
  •  7
  •   gateblues    9 年前

    如果您在Rails上使用Ruby,那么 压扁

    > @title = " abc "
     => " abc " 
    
    > @title.squish
     => "abc"
    > @title
     => " abc "
    
    > @title.squish!
     => "abc"
    > @title
     => "abc" 
    

    如果你用的只是你想用的红宝石

    这就是哥查……在你的情况下,你想使用没有砰!

    一边脱衣!当然,如果没有任何操作返回nil,它仍然会更新变量,所以去掉!不能直接使用。如果你想使用直列条,你可以使用没有爆炸的版本!

    脱衣舞! 使用多线方法

    > tokens["Title"] = " abc "
     => " abc "
    > tokens["Title"].strip!
     => "abc"
    > @title = tokens["Title"]
     => "abc"
    

    单线法…你的答案

    > tokens["Title"] = " abc "
     => " abc "
    > @title = tokens["Title"].strip if tokens["Title"].present?
     => "abc"
    
        5
  •  2
  •   Olly    16 年前

    我认为您的示例是一种明智的方法,尽管您可以将其稍微简化为:

    @title = tokens[Title].strip! || tokens[Title] if tokens[Title]
    

    或者你可以把它放在两行上:

    @title = tokens[Title] || ''
    @title.strip!
    
        6
  •  2
  •   Diogo Neves - Mangaru Jamie Wong    9 年前

    如果在需要类似的方法之后要使用其他方法:

    ( str.strip || str ).split(',')
    

    这样你就可以脱光衣服,之后还能做点什么:)

        7
  •  1
  •   Hauleth    13 年前

    我的方式:

    > (@title = " abc ").strip!
     => "abc" 
    > @title
     => "abc" 
    
        8
  •  1
  •   rewritten    11 年前

    如果您有Ruby1.9或ActiveSupport,您可以简单地

    @title = tokens[Title].try :tap, &:strip!
    

    这真的很酷,因为它利用了 :try 以及 :tap 方法,在我看来,它是Ruby中最强大的函数构造。

    一种更为简洁的形式,传递的功能是作为符号:

    @title = tokens[Title].send :try, :tap, &:strip!
    
        9
  •  -1
  •   Brent    11 年前
    @title = tokens[Title].strip! || tokens[Title]
    

    我完全有可能不理解这个话题,但这不是你需要的吗?

    " success ".strip! || "rescue" #=> "success"
    "failure".strip! || "rescue" #=> "rescue"
    
    推荐文章