Testing whether string starts with or end with another string

RubyString

Ruby Problem Overview


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)

Ruby Solutions


Solution 1 - Ruby

There are built in methods:

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

Solution 2 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMxLDevsView Question on Stackoverflow
Solution 1 - RubyWill RichardsonView Answer on Stackoverflow
Solution 2 - RubysqrcompassView Answer on Stackoverflow