Ruby

Ruby String Contains a Substring

Strings are fundamental building blocks in all programming languages. Every programmer should have a solid understanding of working with strings and perform complex operations on them.

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:

str = '<form action="form.html" method="post">
<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:

$ ruby substring.rb
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:

str = '<form action="form.html" method="get">
<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:

$ ruby substring.rb
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:

str = 'POST'
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:

$ ruby substring.rb
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:

str = 'POST'
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.

$ ruby substring.rb
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:

str = '<form action="form.html" method="post">
<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:

$ ruby substring.rb
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:

str = '<form action="page.html" method="post">…
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:

$ ruby substring.rb
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:

str = 'post'
puts str['post']
puts str['not-found']

If we run the above example, we should see an output as:

=> post
=> 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.

str = 'post'
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:

str = 'post ------ string continues here'
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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list