×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to check a string end with a specified substring

Last Updated : December 15, 2025

Problem Solution

In this program, we will create three strings then we will check a given string starts with a specified substring using the start_with?() function and return a Boolean value.

Program/Source Code

The source code to check a string end with a specified substring is given below. The given program is compiled and executed successfully.

# Ruby program to check a string end 
# with a specified substring

str = "Hello, how are you";
substr1 = "you";
substr2 = "You";

if str.end_with?(substr1)
    print "String 'str' is end with sub-string 'substr1'\n";
else
    print "String 'str' is not end with sub-string 'substr1'\n";
end

if str.end_with?(substr2)
    print "String 'str' is end with sub-string 'substr2'\n";
else
    print "String 'str' is not end with sub-string 'substr2'\n";
end

Output

String 'str' is end with sub-string 'substr1'
String 'str' is not end with sub-string 'substr2'

Explanation

In the above program, we created three strings str, substr1, substr2. Then we used end_with?()  function to check a string end with a specified substring or not and print the appropriate message.

Ruby Strings Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.