Ruby Search and Replace
sub and gsub string methods that use regular expressions, and their in-place variants are
gsub!. The sub & sub! replaces the first occurrence of the pattern and gsub & gsub! replaces all occurrences. All of these methods perform a search-and-replace operation using a Regexp pattern. sub! and gsub! modify the string on which they are called whereas the sub and gsub returns a new string, leaving the original unmodified.
Below is the example to understand it better.
Roll Num : 2004-959-559 Roll Num : 2004959559
In above example, we are using sub! and gsub!. here sub! replace first occurrence of the pattern and gsub! replaces all occurrences.
Geeks for Geeks, is a computer science portal
The gsub! method too can be used with a regular expression.
My Personal Notes arrow_drop_up
Please Login to comment...