ADVERTISEMENT
ADVERTISEMENT

Ruby program to check a string starts with a specified substring

Ruby Example: Write a program to check a string starts with a specified substring.
Submitted by Nidhi, on January 04, 2022

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 starts with a specified substring is given below. The given program is compiled and executed successfully.

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

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

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

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

Output:

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

Explanation:

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

Ruby Strings Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.