How can I check if my string starts with a number?
I'm trying to make this work by using the starts_with ruby method with no luck:
<% if line.start_with?("ANY NUMBER") %>
Thanks!
CodePudding user response:
Just do this :
def starts_with_number?(str)
str.start_with?(*'0'..'9')
end
puts starts_with_number?("123abc") # true
puts starts_with_number?("abc123") # false
puts starts_with_number?("1.23") # true
puts starts_with_number?("-123") # true
CodePudding user response:
In Regular Expressions, \A
means start of string, and \d
means any digit. So this could work for you:
if line.match?(/\A\d/)