如何检查字符串是否包含Ruby中的子字符串?
我有一个字符串变量,内容如下:
varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n""/my/name/is/balaji.so\n""call::myFunction(int const&)\n""void::secondFunction(char const&)\n"..."this/is/last/line/liobrary.so"
在上面的字符串我必须找到一个子字符串即
"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n""/my/name/is/balaji.so\n"
"call::myFunction(int const&)\n"
我该怎么找到它? 我只需要确定子串是否存在。
#1楼
如果大小写无关紧要,那么不区分大小写的正则表达式是一个很好的解决方案:
'aBcDe' =~ /bcd/i # evaluates as true
这也适用于多行字符串。
请参阅Ruby的Regexp类。
#2楼
你也可以这样做......
my_string = "Hello world"if my_string["Hello"]puts 'It has "Hello"'
elseputs 'No "Hello" found'
end# => 'It has "Hello"'
#3楼
三元方式
my_string.include?('ahr') ? (puts 'String includes ahr') : (puts 'String does not include ahr')
要么
puts (my_string.include?('ahr') ? 'String includes ahr' : 'String not includes ahr')
#4楼
比Rails (3.1.0及以上版本)中提供的上述接受的答案更简洁的成语是.in?
。
例如:
my_string = "abcdefg"
if "cde".in? my_stringputs "'cde' is in the String."puts "i.e. String includes 'cde'"
end
我也认为它更具可读性。
cf http://apidock.com/rails/v3.1.0/Object/in%3F
(请注意,它仅在Rails中可用,而不是纯Ruby。)
#5楼
扩展Clint Pachl的答案:
当表达式不匹配时,Ruby中的正则表达式匹配返回nil 。 如果是,则返回匹配发生的字符的索引。 例如
"foobar" =~ /bar/ # returns 3
"foobar" =~ /foo/ # returns 0
"foobar" =~ /zzz/ # returns nil
值得注意的是,在Ruby中只有nil和布尔表达式false求值为false。 其他所有内容(包括空数组,空哈希或整数0)的计算结果为true。
这就是为什么上面的/ foo /示例有效,以及为什么
if "string" =~ /regex/
按预期工作。 如果匹配发生,则仅输入if块的“true”部分。
#6楼
user_input = gets.chomp
user_input.downcase!if user_input.include?('substring')# Do something
end
这将帮助您检查字符串是否包含子字符串
puts "Enter a string"
user_input = gets.chomp # Ex: Tommy
user_input.downcase! # tommyif user_input.include?('s')puts "Found"
elseputs "Not found"
end
#7楼
您可以使用[]
元素字符串引用方法
在[]
内部可以是文字子字符串,索引或正则表达式:
> s='abcdefg'
=> "abcdefg"
> s['a']
=> "a"
> s['z']
=> nil
由于nil
在功能上与false
相同,并且从[]
返回的任何子字符串都为true
您可以像使用方法一样使用逻辑.include?
:
0> if s[sub_s]
1> puts "\"#{s}\" has \"#{sub_s}\""
1> else
1* puts "\"#{s}\" does not have \"#{sub_s}\""
1> end
"abcdefg" has "abc"0> if s[sub_s]
1> puts "\"#{s}\" has \"#{sub_s}\""
1> else
1* puts "\"#{s}\" does not have \"#{sub_s}\""
1> end
"abcdefg" does not have "xyz"
只需确保不要将索引与子字符串混淆:
> '123456790'[8] # integer is eighth element, or '0'
=> "0" # would test as 'true' in Ruby
> '123456790'['8']
=> nil # correct
你也可以使用正则表达式:
> s[/A/i]
=> "a"
> s[/A/]
=> nil
#8楼
如何检查字符串是否包含Ruby中的子字符串?
当你说'check'时,我假设你想要一个返回的布尔值,在这种情况下你可以使用String#match?
。 match?
接受字符串或正则表达式作为其第一个参数,如果它是前者,则它会自动转换为正则表达式。 所以你的用例是:
str = 'string'
str.match? 'strings' #=> false
str.match? 'string' #=> true
str.match? 'strin' #=> true
str.match? 'trin' #=> true
str.match? 'tri' #=> true
String#match?
具有可选的第二个参数的附加好处,该参数指定从中搜索字符串的索引。 默认情况下,此值设置为0
。
str.match? 'tri',0 #=> true
str.match? 'tri',1 #=> true
str.match? 'tri',2 #=> false
#9楼
你可以使用include?
方法:
my_string = "abcdefg"
if my_string.include? "cde"puts "String includes 'cde'"
end
如何检查字符串是否包含Ruby中的子字符串?相关推荐
- 检查数组中的子字符串– Java,Python和Swift
In this quick tutorial, we'll learn how to quickly search a part of a string in an Array. We'll be d ...
- 如何检查数组是否包含JavaScript中的对象?
In this article, we will look at various methods to check if an array includes an object in JavaScri ...
- 在Python中查找子字符串索引的5种方法
在Python中查找字符串中子字符串索引的5种方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() ...
- Python在字符串中查找子字符串
这是小白博主在刷leetcode时遇到的一道题,这是博主近日刷的leetcode题库时结果表现最好的一道题,故在此分享这份喜悦. 希望在以后的日子里可以继续进步,持之以恒. 目录 题目介绍 解题思路及 ...
- python 包含语句_如何检查句子是否包含Python中的某个单词然后执行操作?
根据@knitti的评论,问题是你需要先将句子分成单词,然后检查: term = "message" #term we want to search for input = raw ...
- ajax配套字符串,在ajax请求中填充数据字符串
我有一个表单可以有不同数量的文本字段(包含转换值).他们的名字是例如:"textfield_eng","textfield_ger","textfie ...
- python随机生成字符串_Python 2.6中的随机字符串(可以吗?)
我一直在试图找到一种更像python的方法来生成python中的随机字符串,这种方法也可以伸缩.通常,我看到类似的东西''.join(random.choice(string.letters) for ...
- (转)LinqToSql的连接字符串自动调用配置文件中的连接字符串
LinqToSql会把连接字符串散落地写在多个位置,并且编译后就固定不可更改了. 于是带来了各种不方便.比如在一台机器上配置好的程序拿到另一台机器上,就不得不找出所有散落的连接字符串,然后一一修改才能 ...
- python取字符串一部分_python,如何获取字符串中的子字符串,部分字符串
说明: 比如有一个字符串,python,如何就获取前3位,或者后2位.在此记录下. 操作过程: 1.通过分割符的方式,下标的方式,获取字符串中的子串 >>> text = 'pyth ...
最新文章
- java pathinfo,nginx下支持PATH_INFO的方法实例详解
- java字符串类型常量拼接与变量拼接的区别
- CentOS7上OpenResty安装
- 云炬随笔20171209
- Django+Echarts画图实例
- linux各种查看端口号
- 每天一道LeetCode-----链表插入排序
- P3702-[SDOI2017]序列计数【矩阵乘法】
- python2.7安装pygame_python 安装 pygame了
- MODBUS通讯:libmodbus库使用方法
- Python 这么热,运维要不要追赶 Python 的热潮?
- shell读取用户输入
- 如何解决类似 curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused 的问题 #10
- 【CASS精品教程】CASS 9.2绘制地籍图详细实验案例教程
- mysql 多表联合查询
- Java找不到对象的警告_程序猿的24个段子:程序员找不到对象 一般有三种情况...
- Js学习心得和思考方法
- csgo开箱网站有哪些?NEW
- 5大顶级移动UI开发框架
- 论文解读:NSGA-II, EFR, EFR-RR