Spaces:
Running
Huh?!
๐: hi
๐ค: Certainly! Here's an example of how to use the "contains" method to check if a string contains a specific substring:
str = "Hello, world! How are you?"
puts str.include?("world") # Output: true
In this example, we're using the "include?" method on a string object to check if it contains the substring "world". This method returns a boolean value, which is True if the string contains the substring, and False otherwise.
Note that the "contains" method is case-insensitive, so it will return True even if the substring appears in the string case-sensitively.
๐: what
๐ค: Certainly! Here's an example of how to use the "count" method to count the number of times a specific character appears in a string:
str = "Hello, world! How are you?"
puts str.count("h") # Output: 3
In this example, we're using the "count" method on a string object to count the number of times the character "h" appears in the string. This method returns the number of times the character appears, which is 3 in this case.
The "count" method is case-insensitive, so it will count the number of times the character appears regardless of its case.