ruby字符串查找

String#index/rindex/include?/sub/gsub

 

----------------------------------------------------------   String #index
str .index(substring [, offset])    =>  fixnum  or  nil
str .index(fixnum [, offset])       =>  fixnum  or  nil
str .index(regexp [, offset])       =>  fixnum  or  nil
-----------------------------------------------------------------------
Returns the index 
of  the first occurrence  of  the given substring, character (fixnum),  or  pattern (regexp)  in   str . Returns nil  if   not  found.  If  the  second  parameter  is  present, it specifies the position  in  the  string   to  begin the search.

   
" hello " .index( ' e')             #=> 1
    " hello " .index( ' lo')            #=> 3
    " hello " .index( ' a')             #=> nil
    " hello " .index( 101 )             # =>   1
   
" hello " .index( / [aeiou] / - 3 )   # =>   4

 

 

---------------------------------------------------------   String #rindex
str .rindex(substring [, fixnum])    =>  fixnum  or  nil
str .rindex(fixnum [, fixnum])    =>  fixnum  or  nil
str .rindex(regexp [, fixnum])    =>  fixnum  or  nil
-----------------------------------------------------------------------
Returns the index 
of  the last occurrence  of  the given substring, character (fixnum),  or  pattern (regexp)  in   str . Returns nil  if   not  found.  If  the  second  parameter  is  present, it specifies the position  in  the  string   to   end  the search --- characters beyond this point will  not  be considered.

   
" hello " .rindex( ' e')             #=> 1
    " hello " .rindex( ' l')             #=> 3
    " hello " .rindex( ' a')             #=> nil
    " hello " .rindex( 101 )             # =>   1
   
" hello " .rindex( / [aeiou] / - 2 )   # =>   1

 

 

-------------------------------------------------------   String #include?
str .include? other_str    =>   true   or   false
str .include? fixnum       =>   true   or   false
-----------------------------------------------------------------------
Returns 
true   if   str  contains the given  string   or  character.

   
" hello " .include?  " lo "    # =>   true
   
" hello " .include?  " ol "    # =>   false
   
" hello " .include? ?h     # =>   true

 

 

------------------------------------------------------------   String # sub
str.sub(pattern, replacement)         => new_str
str.sub(pattern) {|match| block }     => new_str
-----------------------------------------------------------------------
Returns a copy 
of str with the first occurrence of pattern replaced with either replacement or the value of the block. The pattern will typically be a Regexp; if it is a String then no regular expression metacharacters will be interpreted (that is /d/ will match a digit, but 'd' will match a backslash followed by a 'd').

If the method call specifies replacement, special variables such as $& will not be useful, as substitution into the string occurs before the pattern match starts. However, the sequences 12, etc., may be used.

In the block form, the current match string is passed in as a parameter, and variables such as $1, $2, $`, $&and $' will be set appropriately. The value returned by the block will be substituted for the match on each call.

The result 
inherits any tainting in the original string or any supplied replacement string.

   
"hello".sub(/[aeiou]/'*')               #=> "h*llo"
   "hello".sub(/([aeiou])/'<>')          #=> "h<e>llo"
   "hello".sub(/./) {|s| s[0].to_s + ' ' }   #=> "104 ello"

 

 

-----------------------------------------------------------   String #gsub
str .gsub(pattern, replacement)        =>  new_str
str .gsub(pattern) {|match| block }    =>  new_str
-----------------------------------------------------------------------
Returns a copy 
of   str   with  all occurrences  of  pattern replaced  with  either replacement  or  the value  of  the block. The pattern will typically be a Regexp;  if  it  is  a  String   then  no regular expression metacharacters will be interpreted (that  is   / d /  will match a digit, but  ' d' will match a backslash followed by a 'd').

If  a  string   is  used  as  the replacement, special variables from the match (such  as  $ &   and  $ 1 ) cannot be substituted into it,  as  substitution into the  string  occurs before the pattern match starts. However, the sequences  1 2 and  so  on  may be used  to  interpolate successive groups  in  the match.

In  the block form, the current match  string   is  passed  in   as  a parameter,  and  variables such  as  $ 1 , $ 2 , $`, $ & and  $ '  will be set appropriately. The value returned by the block will be substituted for the match on each call.

The result 
inherits  any tainting  in  the original  string   or  any supplied replacement  string .

   
" hello " .gsub( / [aeiou] / ' *')              #=> "h*ll*"
    " hello " .gsub( / ([aeiou]) / ' <>')         #=> "h<e>ll<o>"
    " hello " .gsub( / . / ) {|s| s[ 0 ].to_s  +   '  '}   #=> "104 101 108 108 111 "

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值