ruby字符串截取字符串
检查字符串中的子字符串 (Checking substring in a string)
Given a string and substring, we have to check whether a string contains a substring or not.
给定字符串和子字符串,我们必须检查字符串是否包含子字符串。
A substring is a sequence of characters within a string or in other words, a substring is a part of the string. Ruby provides you a keyword through which you can check the availability of a substring. That keyword is known by ".include?" which takes the specified substring as the parameter under double quotes like .include? “Hello”.
子字符串是字符串中的字符序列,换句话说,子字符串是字符串的一部分。 Ruby为您提供了一个关键字,通过该关键字可以检查子字符串的可用性 。 该关键字被称为“ .include?”。 这将指定的子字符串作为双引号(如.include)下的参数? “你好”。
Methods used:
使用的方法:
puts: The method put string is used to put a message on the screen for the user.
puts :put字符串方法用于向用户在屏幕上显示消息。
gets: This method is used to take input from the user.
gets :此方法用于接收用户的输入。
.include?: One of the functionalities of this keyword is that it is used to analyze the existence of a substring within a string.
。包括? :此关键字的功能之一是用于分析字符串中子字符串的存在。
Variables used:
使用的变量:
str1: It is being employed for storing the actual string.
str1 :用于存储实际的字符串。
substr2: It is storing the substring to be checked which is provided by the user.
substr2 :存储用户提供的要检查的子字符串。
Ruby code to check whether a string contains a substring or not
Ruby代码检查字符串是否包含子字符串
=begin
Ruby program to check whether a string
contains substring or not
=end
puts "Enter the string:"
str1=gets.chomp
puts "Enter the substring:"
substr1=gets.chomp
if str1.include? substr1
puts "Substring Found"
else
puts "substring Not Found"
end
Output
输出量
Run 1:
Enter the string:
My name is Hrithik
Enter the substring:
saksham
substring Not Found
Run 2:
Enter the string:
You are at Includehelp.com
Enter the substring:
at
Substring Found
翻译自: https://www.includehelp.com/ruby/check-whether-a-string-contains-substring-or-not.aspx
ruby字符串截取字符串