MxLDevs
MxLDevs

Reputation: 19526

Testing whether string starts with or end with another string

How should I check whether a string starts or ends with a given string? There doesn't seem to be any built-in methods available (or maybe it's just the IDE I'm using that isn't having it show up: RDE)

Upvotes: 96

Views: 50591

Answers (2)

sqrcompass
sqrcompass

Reputation: 671

string.start_with?("substring")
string.end_with?("substring")

Returns true or false

Source:

http://ruby-doc.org//core-2.2.0/String.html#method-i-start_with-3F

http://ruby-doc.org//core-2.2.0/String.html#method-i-end_with-3F

Upvotes: 9

Will Richardson
Will Richardson

Reputation: 7970

There are built in methods:

"String".start_with? "S" # true
"String".end_with? "4" # false

Upvotes: 181

Related Questions