How to check whether a string contains a substring in Ruby

后端 未结 9 937
南旧
南旧 2020-11-28 18:02

I have a string variable with content:

varMessage =   
            \"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\\n\"


            \"/my/name/is/balaji.so\\n\"
                 


        
相关标签:
9条回答
  • 2020-11-28 18:03

    How to check whether a string contains a substring in Ruby?

    When you say 'check', I assume you want a boolean returned in which case you may use String#match?. match? accepts strings or regexes as its first parameter, if it's the former then it's automatically converted to a regex. So your use case would be:

    str = 'string'
    str.match? 'strings' #=> false
    str.match? 'string'  #=> true
    str.match? 'strin'   #=> true
    str.match? 'trin'    #=> true
    str.match? 'tri'     #=> true
    

    String#match? has the added benefit of an optional second argument which specifies an index from which to search the string. By default this is set to 0.

    str.match? 'tri',0   #=> true
    str.match? 'tri',1   #=> true
    str.match? 'tri',2   #=> false
    
    0 讨论(0)
  • 2020-11-28 18:05

    Expanding on Clint Pachl's answer:

    Regex matching in Ruby returns nil when the expression doesn't match. When it does, it returns the index of the character where the match happens. For example:

    "foobar" =~ /bar/  # returns 3
    "foobar" =~ /foo/  # returns 0
    "foobar" =~ /zzz/  # returns nil
    

    It's important to note that in Ruby only nil and the boolean expression false evaluate to false. Everything else, including an empty Array, empty Hash, or the Integer 0, evaluates to true.

    That's why the /foo/ example above works, and why.

    if "string" =~ /regex/
    

    works as expected, only entering the 'true' part of the if block if a match occurred.

    0 讨论(0)
  • 2020-11-28 18:07

    A more succinct idiom than the accepted answer above that's available in Rails (from 3.1.0 and above) is .in?:

    my_string = "abcdefg"
    if "cde".in? my_string
      puts "'cde' is in the String."
      puts "i.e. String includes 'cde'"
    end
    

    I also think it's more readable.

    See the in? documentation for more information.

    Note again that it's only available in Rails, and not pure Ruby.

    0 讨论(0)
  • 2020-11-28 18:08

    If case is irrelevant, then a case-insensitive regular expression is a good solution:

    'aBcDe' =~ /bcd/i  # evaluates as true
    

    This will also work for multi-line strings.

    See Ruby's Regexp class for more information.

    0 讨论(0)
  • 2020-11-28 18:12

    You can use the include? method:

    my_string = "abcdefg"
    if my_string.include? "cde"
       puts "String includes 'cde'"
    end
    
    0 讨论(0)
  • 2020-11-28 18:18

    You can use the String Element Reference method which is []

    Inside the [] can either be a literal substring, an index, or a regex:

    > s='abcdefg'
    => "abcdefg"
    > s['a']
    => "a"
    > s['z']
    => nil
    

    Since nil is functionally the same as false and any substring returned from [] is true you can use the logic as if you use the method .include?:

    0> if s[sub_s]
    1>    puts "\"#{s}\" has \"#{sub_s}\""
    1> else 
    1*    puts "\"#{s}\" does not have \"#{sub_s}\""
    1> end
    "abcdefg" has "abc"
    
    0> if s[sub_s]
    1>    puts "\"#{s}\" has \"#{sub_s}\""
    1> else 
    1*    puts "\"#{s}\" does not have \"#{sub_s}\""
    1> end
    "abcdefg" does not have "xyz" 
    

    Just make sure you don't confuse an index with a sub string:

    > '123456790'[8]    # integer is eighth element, or '0'
    => "0"              # would test as 'true' in Ruby
    > '123456790'['8']  
    => nil              # correct
    

    You can also use a regex:

    > s[/A/i]
    => "a"
    > s[/A/]
    => nil
    
    0 讨论(0)
提交回复
热议问题