Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

String#start_with?

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

Returns whether self starts with any of the given patterns.

For each argument, the pattern used 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

Related: see Querying.

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