A typical operation when working with strings is determining if a string contains a specific substring. You can use these methods to locate and sanitize input from users, thus preventing any malicious code.
In this tutorial, we will show you how to determine if a string contains a specific substring.
Ruby Include? Method
The easiest way to find if a specific substring is in another string is to use include? built-in method.
Here is an example:
<label>Info: <input type="text" name="hello" dirname="hello.dir" value="linuxhint"></label>
<input type="submit"/>
</form>'
if str.include?("post")
puts "correct HTTP method -> POST"
else
puts "Incorrect HTTP method"
end
In the example above, we have a simple program containing information from an HTML form input. We check if the form contains a post method using the include method.
Since the method’s return value is a boolean true or false if the substring is available or not, respectively, we implement an if..else statement to act accordingly.
The example output from the above script is as shown:
correct HTTP method -> POST
If we substitute the form data and set the method to a get, we can get to the else block as:
<label>Info: <input type="text" name="hello" dirname="hello.dir" value="linuxhint"></label>
<input type="submit"/>
</form>'
if str.include?("post")
puts "correct HTTP method -> POST"
else
puts "Incorrect HTTP method"
end
We run the script as:
Incorrect HTTP method
One drawback of the include? method is it’s case-sensitive. This means POST and post will return a false value.
For example:
if str.include?("post")
puts "correct HTTP method -> POST"
else
puts "Incorrect HTTP method"
end
If we run the above example, we should get an output from the else block as:
Incorrect HTTP method
To overcome this, we can first convert the entire string to lower case or uppercase, then verify if the substring is within the main string.
Here is an example:
if (str.include?("post".upcase))
puts "correct HTTP method -> POST"
else
puts "Incorrect HTTP method"
end
In this example, we convert the substring to uppercase, which matches the main string, and check the value.
correct HTTP method -> POST
Using RegEx
We can use RegEx to check if a string holds a specific substring. Ruby will return nil if the specified regular expression pattern is not within in the string.
For example:
<label>Info: <input type="text" name="hello" dirname="hello.dir" value="linuxhint"></label>
<input type="submit"/>
</form>'
puts str =~ /post/
In the example above, we use a simple regular expression to check if the string contains the substring “post”.
If we run the script above, we should get an output similar to the one below:
33
The return value of the regular expression is the initial index where the match case is found. In our example, the post string is starting from index 33. We can verify as:
puts str[33..36]
In the above example, we start from the initial index where the string is found to index matching the length of the substring:
post
Ruby String Element Reference []
We can also use the Ruby string reference method. We can pass either a string, an index value, or a regular expression inside a pair of square brackets.
Example usage:
puts str['post']
puts str['not-found']
If we run the above example, we should see an output as:
=> nil
If the substring is available, it will return the actual string; however, it will return nil if there is no substring.
The same case applies to regular expressions.
puts str[/post/]
=> post
We can also use the method above to extract a substring. For example, to extract 10 characters after the match, we can do:
puts str[str =~ /string/,9]
The example above will fetch 9 indexes after the index where the match is.
Conclusion
In this guide, you learned how to check if a string contains a specific substring using the include? method, regular expressions, and string element reference.