ADVERTISEMENT
ADVERTISEMENT

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 »


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.