Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

ruby - 如何检查字符串是否包含Ruby中的子字符串?(How to check whether a string contains a substring in Ruby?)

I have a string variable with content as follows:

(我有一个字符串变量,内容如下:)

varMessage =   
            "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn
"


            "/my/name/is/balaji.so
"
            "call::myFunction(int const&)
"
            "void::secondFunction(char const&)
"
             .
             .
             .
            "this/is/last/line/liobrary.so"

in above string i have to find a sub string ie

(在上面的字符串我必须找到一个子字符串即)

"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn
"


"/my/name/is/balaji.so
"
"call::myFunction(int const&)
"

How can I find it?

(我该怎么找到它?)

I just need to determine whether the substring is present or not.

(我只需要确定子串是否存在。)

  ask by BSalunke translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the include?

(你可以使用include?)

method:

(方法:)

my_string = "abcdefg"
if my_string.include? "cde"
   puts "String includes 'cde'"
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...