String#include?

Added in v1.8 · Updated March 14, 2026 · String Methods
ruby string methods

String#include? checks whether a substring exists within a string. It returns a boolean value, making it straightforward for conditional logic throughout your Ruby code. This method has been available since Ruby 1.8, making it one of the oldest and most reliable string methods in the language.

Signature

string.include?(other_str) -> true or false
ParameterTypeDescription
other_strStringThe substring to search for

The method takes a single argument: the substring you want to find. It does not accept a regular expression.

Code Examples

Basic substring check

text = "Hello, World!"

text.include?("Hello")   # => true
text.include?("World")   # => true
text.include?("Ruby")   # => false

Case-sensitive matching

email = "user@example.com"

email.include?("example")   # => true
email.include?("EXAMPLE")  # => false (case matters)
email.include?("@")         # => true

This is important to remember: include? performs exact character matching. If you need case-insensitive checking, convert both strings to the same case first:

email.upcase.include?("EXAMPLE")  # => true when email is "user@example.com"

Using in conditional statements

filename = "report.pdf"

if filename.include?(".pdf")
  puts "Processing PDF file"
  # Output: Processing PDF file
end

if filename.include?(".doc")
  puts "Processing Word document"
  # No output - condition is false
end

This pattern is common for file type detection. You can chain multiple checks for more complex logic.

Checking for multiple substrings

message = "Please contact support@example.com for help"

message.include?("contact")   # => true
message.include?("http")      # => false
message.include?("@")         # => true

Practical validation example

def valid_username?(username)
  return false if username.include?(" ")
  return false if username.include?("@")
  username.length >= 3
end

valid_username?("john_doe")   # => true
valid_username?("john doe")   # => false (contains space)
valid_username?("ab")         # => false (too short)

Return Value

Returns true if the string contains the given substring anywhere within it, false otherwise. The search is case-sensitive, meaning “A” and “a” are considered different characters. The method stops searching as soon as it finds a match, so performance is proportional to string length.

Edge Cases

Empty string

"".include?("")    # => true (empty string is always found)
"hello".include?("")   # => true

This behavior follows the mathematical definition where an empty set is a subset of all sets. While technically true, checking for an empty substring is rarely useful in practice.

Nil argument

"hello".include?(nil)
# => NoMethodError: undefined method `include?' for "hello":String

Passing nil raises a NoMethodError because nil does not have a to_s conversion in this context. Always ensure your argument is a string before calling include?.

Special characters

"price: $50".include?("$")   # => true
"a\tb".include?("\t")        # => true (tabs are matched)
"line1\nline2".include?("\n") # => true (newlines are matched)

The method handles all ASCII and Unicode characters correctly, including whitespace, punctuation, and special symbols.

Single character vs multi-character

"hello".include?("l")    # => true (single character works)
"hello".include?("ll")   # => true (multiple characters work)
"hello".include?("lo")  # => true (adjacent characters work)

Performance Notes

For very large strings, include? performs a linear search. If you need to check for many different substrings repeatedly, consider using a Set or a Trie data structure instead. For most everyday use cases, the performance is more than adequate.

See Also