Ruby 需要子字符串解释
Ruby substring explanation needed
我有两个字符串 str1 = 'abbab'
和 str2 = 'ba'
。如果我这样做
str1.include? str2
我得到 true
。当我做
str2.include? str1
为什么我得到 false
?
如果 str2
位于 2
位置,即 str2
是否是 str1
的子字符串,即 str1[2..3] == str2
?
什么是子串?
A substring of a string is another string that occurs "in".
按照上述定义:
'ba'
出现在 'abbab'
中,因此 'ba'
是 'abbab'
的子字符串。
现在反过来看:
'abbab'
是否出现在 'ba'
内?不,所以 'abbab'
不是 'ba'
.
的子串
How should I find if str2
is a substring of str1
?
通过做:
str1.include? str2 #true
How can I find str1
is a substring of str2
?
str2.include? str1 #false
# since its not, you are getting false.
Read the documentation carefully
:
include? other_str → true or false
Returns true
if str contains the given string or character.
示例:
=> "foobar".include? "bar"
#> true
=> "bar".include? "foobar"
#> false
我有两个字符串 str1 = 'abbab'
和 str2 = 'ba'
。如果我这样做
str1.include? str2
我得到 true
。当我做
str2.include? str1
为什么我得到 false
?
如果 str2
位于 2
位置,即 str2
是否是 str1
的子字符串,即 str1[2..3] == str2
?
什么是子串?
A substring of a string is another string that occurs "in".
按照上述定义:
'ba'
出现在 'abbab'
中,因此 'ba'
是 'abbab'
的子字符串。
现在反过来看:
'abbab'
是否出现在 'ba'
内?不,所以 'abbab'
不是 'ba'
.
How should I find if
str2
is a substring ofstr1
?
通过做:
str1.include? str2 #true
How can I find
str1
is a substring ofstr2
?
str2.include? str1 #false
# since its not, you are getting false.
Read the documentation carefully
:
include? other_str → true or false
Returns
true
if str contains the given string or character.
示例:
=> "foobar".include? "bar"
#> true
=> "bar".include? "foobar"
#> false