Home »
Ruby Tutorial »
Ruby Programs
Ruby program to check a string is a numeric string or not using the match() function
Last Updated : December 15, 2025
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 »
Advertisement
Advertisement