Q:
Ruby program to replace number within a string using gsub() method with regular expression
belongs to collection: Ruby Strings Programs
Ruby Strings Programs
- Ruby program to illustrate the creation of strings
- Ruby program to illustrate the difference between single quote string and double-quote string
- Ruby program to create a String object
- Ruby program to find the length of a string
- Ruby program to get characters from a string using the index
- Ruby program to get a substring from the string
- Ruby program to demonstrate the example to write double-quotes in a string
- Ruby program to demonstrate the escape sequence characters
- Ruby program to demonstrate the string concatenation
- Ruby program to make string immutable
- Ruby program to compare two strings using the Equal To (==) operator
- Ruby program to compare two strings using the eql?() method
- Ruby program to compare two strings by ignoring case
- Ruby program to check the given string is empty or not
- Ruby program to demonstrate the string interpolation
- Ruby program to check a string contains a specified substring or not
- Ruby program to get the index of the substring in a string
- Ruby program to pad a string with 0\'s on left side
- Ruby program to pad a string with 0\'s on the right side
- Ruby program to convert a string into uppercase
- Ruby program to convert a string into lowercase
- Ruby program to trim a string from both sides
- Ruby program to trim a string from the left side
- Ruby program to trim a string from the right side
- Ruby program to check a string starts with a specified substring
- Ruby program to check a string end with a specified substring
- Ruby program to delete suffix substring from the given string
- Ruby program to delete prefix substring from the given string
- Ruby program to convert the string into an array of characters
- Ruby program to convert the string into an array of characters based on the specified character
- Ruby program to convert the array of characters into the string
- Ruby program to convert an integer array into a string
- Ruby program to convert a numeric string into integer using to_i() function
- Ruby program to check a string is a numeric string or not using the match() function
- Ruby program to concatenate the strings using the << operator
- Ruby program to iterate characters using the each_char() function
- Ruby program to convert the string into an array of characters using chars() function
- Ruby program to create a multi-line string using \'%Q\'
- Ruby program to replace text inside the string using gsub() method
- Ruby program to remove a specified character inside the string using gsub() method
- Ruby program to replace number within a string using gsub() method with regular expression
- Ruby program to convert the string into title case using gsub() method
- Ruby program to remove the last specified character from the string
- Ruby program to count a specified character inside the string
Program/Source Code:
The source code to replace a number within a string using the gsub() method with a regular expression is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created a string str initialized with "There are 4 persons.". Then we replaced the number inside the string using the gsub() method with regular expression. After that, we printed the updated string.
need an explanation for this answer? contact us directly to get an explanation for this answer