Ruby | String index Method Last Updated : 09 Dec, 2019 Comments Improve Suggest changes Like Article Like Report index is a String class method in Ruby which is used to returns the index of the first occurrence of the given substring or pattern (regexp) in the given string. It specifies the position in the string to begin the search if the second parameter is present. It will return nil if not found. Syntax: str.index() Parameters: Here, str is the given string. Returns: Index of the first occurrence of the given substring or pattern (regexp) in str. Example 1: Ruby # Ruby program to demonstrate # the index method # Taking a string and # using the method puts "Sample".index('m') puts "Program".index('gr') puts "Checking".index('a') Output: 2 3 Example 2: Ruby # Ruby program to demonstrate # the index method # Taking a string and # using the method puts "Mangal".index(?g) puts "Language".index(/[aeiou]/, -3) Output: 3 5 Comment More infoAdvertise with us Next Article Ruby | String index Method K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby String-class Similar Reads Ruby | String hex Method hex is a String class method in Ruby which is used to treats the leading characters from the given string as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. Syntax: str.hex Parameters: Here, str is the given s 1 min read Ruby | String insert Method insert is a String class method in Ruby which is used to inserts the specified string before the character at the given index, modifying the given one. Negative indices count from the end of the string, and insert after the given character. Syntax: str.insert(index, other_str) Parameters: Here, str 1 min read Ruby | String inspect Method inspect is a String class method in Ruby which is used to return a printable version of the given string, surrounded by quote marks, with special characters escaped. Syntax: str.inspect Parameters: Returns: Here, str is the given string. Example 1: Ruby # Ruby program to demonstrate # the inspect me 1 min read Ruby | String eql? Method eql? is a String class method in Ruby which is used to check whether the strings are equal or not if they have the same length and content. Syntax: str.eql?(Other_str) Parameters: Here, str and other_str are the strings. Returns: True or false basis on the equality. Example 1: Ruby # Ruby program to 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 delete() Method delete is a String class method in Ruby which is used to return a copy of the given string with all characters in the intersection of its arguments deleted. Syntax: str.delete(parameter_list) Parameters: Here, str is the given string and parameter_list are the specified characters. Returns: A new co 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 delete! Method delete! is a String class method in Ruby which is used to perform a delete operation, returning the given string, or nil if the given string was not modified. Syntax: str.delete!(parameter_list) Parameters: Here, str is the given string and parameter_list are the specified characters. Returns: nil i 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 Like