Ruby | String gsub Method Last Updated : 12 Dec, 2019 Comments Improve Suggest changes Like Article Like Report gsub is a String class method in Ruby which is used to return a copy of the given string with all occurrences of pattern substituted for the second argument. Syntax: str.gsub(pattern, replacement) Parameters: Here, str is the given string. pattern may be specified regex or character set to be removed. replacement is the set of characters which is to be put. Returns:A copy of the string with all occurrences of pattern substituted for the second argument. Example 1: Ruby # Ruby program to demonstrate # the gsub method # Taking a string and # using the method puts "Sample".gsub(/[amuyt]/, '*') puts "Program".gsub(/([gmra])/, '<\1>') Output: S**ple Po Example 2: Ruby # Ruby program to demonstrate # the gsub method # Taking a string and # using the method puts "Ruby".gsub(/[tyru]/, '<\1>') puts "String".gsub(/([igtr])/, '*') Output: Rb S***n* Comment More infoAdvertise with us Next Article Ruby | String gsub Method K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby String-class Similar Reads Ruby | String gsub! Method gsub! is a String class method in Ruby which is used to return a copy of the given string with all occurrences of pattern substituted for the second argument. If no substitutions were performed, then it will return nil. If no block and no replacement is given, an enumerator is returned instead. Synt 1 min read Ruby | String * Method String# * is a String class method in Ruby which is used to returns a new String containing integer copies of the receiver. Here, integer must be greater than or equal to 0. Syntax: str * Integer Parameters: Here, str is the required string and integer is the number of copies. Returns: This method r 1 min read Ruby | String =~ Method =~() is a String class method in Ruby which is used for matching purpose. If the given object is a Regexp, then this method will use it as a pattern to match against the given string. Syntax: str =~ obj Parameters: Here, str is the given string and obj is the object to be matched. Returns: The posit 1 min read Ruby | String + Method +() is a String class method in Ruby which is used to return a new string that contains the other specified string concatenated to the given string. Syntax: str + other_str Parameters: Here, str and other_str are the strings which are to be concatenated. Returns: Returns the concatenated string. Exa 1 min read Ruby | String == method ==() is a String class method in Ruby which is used to check whether str==obj or not. Syntax: str == obj Parameters: Here, str is the given string and obj is the object to be compared. Returns: True or False based on the equality of the two strings. Example 1: Ruby #ruby 2.3.1 # Ruby program to demo 1 min read Ruby | String === Method ===() is a String class method in Ruby which is used to check whether str==obj or not. This method is similar to == method of string class. Syntax: str === obj Parameters: Here, str is the given string and obj is the object to be compared. Returns: True or False based on the equality of the two stri 1 min read Ruby | String hash method hash is a String class method in Ruby which is used to return a hash based on the string's length, content and encoding. Syntax: str.hash Parameters: Here, str is the given string. Returns: A hash based on the string's length, content and encoding. Example 1: Ruby # Ruby program to demonstrate # the 1 min read Ruby | String dump Method dump is a String class method in Ruby which is used to generate a version of the given string with all non-printing characters replaced by \nnn notation and all special characters escaped. Syntax: str.dump Parameters: Here, str is the given string. Returns: A new string with all non-printing charact 1 min read Ruby | String getbyte Method getbyte is a String class method in Ruby which is used to return the indexth byte as an integer. Syntax: str.getbyte(index) Parameters: Here, str is the given string. Returns: Indexth byte as an integer. Example 1: Ruby # Ruby program to demonstrate # the getbyte method # Taking a string and # using 1 min read Ruby | String chars() Method chars is a String class method in Ruby which is used to return an array of characters in str. Syntax: str.chars Parameters: Here, str is the given string Returns: An array of the characters. Example 1: Ruby # Ruby program to demonstrate # the chars method # Taking a string and # using the method put 1 min read Like