Q:

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

belongs to collection: Ruby Strings Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 str1str2, 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Strings Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to concatenate the strings using the ... >>
<< Ruby program to convert a numeric string into inte...