To check if a string ends with a specific suffix in Ruby, you can use the end_with?
method.
In this example,
str1
with a value of "Hello World"
.end_with?
method with the suffix "World"
.end_with?
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.str1 = "Hello World"
ends_with_world = str1.end_with?("World")
puts "String ends with suffix 'World': #{ends_with_world}"
String ends with suffix 'World': true
In this example,
str2
with a value of "apple"
.end_with?
method with the suffix "banana"
.end_with?
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.str2 = "apple"
ends_with_banana = str2.end_with?("banana")
puts "String ends with suffix 'banana': #{ends_with_banana}"
String ends with suffix 'banana': false
In this tutorial, we learned How to Check if String ends with Specific Suffix in Ruby language with well detailed examples.