How to check string contains special character in ruby

前端 未结 7 1701
我在风中等你
我在风中等你 2021-02-12 22:31

How to check whether a string contains special character in ruby. If I get regular expression also it is fine.

Please let me know

相关标签:
7条回答
  • 2021-02-12 23:28

    How about this command in Ruby 2.0.0 and above?

    def check_for_a_special_charachter(string)   
      /\W/ === string
    end
    

    Therefore, with:

    !"He@llo"[/\W/].nil?  => True
    
    !"Hello"[/\W/].nil?   => False
    
    0 讨论(0)
提交回复
热议问题