Ruby program to check a string is a numeric string or not using the match() function

Ruby Example: Write a program to check a string is a numeric string or not using the match() function.
Submitted by Nidhi, on January 06, 2022

Problem Solution:

In this program, we will create two strings. Then we will check strings are numeric strings or not using the match() function.

Program/Source Code:

The source code to check a string is a numeric string or not using the match() function is given below. The given program is compiled and executed successfully.

# Ruby program to check a string is a numeric string 
# or not using match() function

str1 = "4536";
str2 = "ABC4536";

if str1.match?(/\A-?\d+\Z/)
    printf "str1 is a numeric string.";
else
    printf "str1 is not a numeric string.";
end

if str2.match?(/\A-?\d+\Z/)
    printf "str2 is a numeric string.";
else
    printf "str2 is not a numeric string.";
end

Output:

str1 is a numeric string.
str2 is not a numeric string.

Explanation:

In the above program, we created two strings str1, str2, that are initialized with "4536", "ABC4536". Then we used the match() function to check the given strings are numeric strings or not. After that, we printed the appropriate message.

Ruby Strings Programs »




Comments and Discussions!

Load comments ↻





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