Check if string starts with letter X in Ruby

How to check that a string starts with a specified letter in Ruby?

+3
source share
2 answers

You can use the String#start_with?method

'String'.start_with? 'S'
 => true

You can also indicate possible start options.

'String'.start_with? 'E', 'S'
 => true
+16
source

you can use regex

str.match /^X/
+1
source

All Articles