Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#start_with?

(*::Regexp | ::string prefixes) -> bool
start_with?(*string_or_regexp) → true or false

Returns whether self starts with any of the given string_or_regexp.

Matches patterns against the beginning of self. For each given string_or_regexp, the pattern is:

Returns true if any pattern matches the beginning, false otherwise:

Example
'hello'.start_with?('hell')               # => true
'hello'.start_with?(/H/i)                 # => true
'hello'.start_with?('heaven', 'hell')     # => true
'hello'.start_with?('heaven', 'paradise') # => false
'тест'.start_with?('т')                   # => true
'こんにちは'.start_with?('こ')              # => true

Related: String#end_with?.

See also #squeeze! #strip #squeeze #strip! also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0