日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Ruby字符串處理函數(shù)總結列表分享

Ruby語言對于編程人員來說是一項非常有用的函數(shù)。學好這項語言可以幫助我們實現(xiàn)簡便輕松的編寫方式。下面我們向大家介紹一下Ruby字符串處理函數(shù)的一些基本概念。#t#

創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、哈爾濱網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5高端網(wǎng)站建設、成都做商城網(wǎng)站、集團公司官網(wǎng)建設、外貿網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為哈爾濱等各大城市提供網(wǎng)站開發(fā)制作服務。

Ruby字符串處理函數(shù)1.返回字符串的長度

  1. str.length => integer 

Ruby字符串處理函數(shù)2.判斷字符串中是否包含另一個串

  1. str.include? other_str
     => true or false  
  2. "hello".include? "lo"
     #=> true  
  3. "hello".include? "ol"
     #=> false  
  4. "hello".include? ?h
     #=> true 

Ruby字符串處理函數(shù)3.字符串插入:

 
 
 
  1. str.insert(index, other_str) 
    => str  
  2.  
  3. "abcd".insert(0, 'X')
     #=> "Xabcd"  
  4. "abcd".insert(3, 'X')
     #=> "abcXd"  
  5. "abcd".insert(4, 'X') 
    #=> "abcdX"  
  6. "abcd".insert(-3, 'X')   
  7.  
  8. -3, 'X') #=> "abXcd"  
  9. "abcd".insert(-1, 'X')
     #=> "abcdX" 

Ruby字符串處理函數(shù)4.字符串分隔,默認分隔符為空格

 
 
 
  1. str.split(pattern=$;, [limit])
     => anArray  
  2.  
  3. " now's the time".split #=> 
    ["now's", "the", "time"]  
  4. "1, 2.34,56, 7".split(%r{,\s*})
     #=> ["1", "2.34", "56", "7"]  
  5. "hello".split(//) #=> 
    ["h", "e", "l", "l", "o"]  
  6. "hello".split(//, 3) #=> 
    ["h", "e", "llo"]  
  7. "hi mom".split(%r{\s*}) #=> 
    ["h", "i", "m", "o", "m"]  
  8.  
  9. "mellow yellow".split("ello")
     #=> ["m", "w y", "w"]  
  10. "1,2,,3,4,,".split(',') #=>
     ["1", "2", "", "3", "4"]  
  11. "1,2,,3,4,,".split(',', 4)
     #=> ["1", "2", "", "3,4,,"] 

Ruby字符串處理函數(shù)5.字符串替換

 
 
 
  1. str.gsub(pattern, replacement)
     => new_str  
  2. str.gsub(pattern) {|match| block } 
    => new_str  
  3. "hello".gsub(/[aeiou]/, '*') 
    #=> "h*ll*" #將元音替換成*號  
  4. "hello".gsub(/([aeiou])/, '<\1>') 
    #=> "hll" #將元音加上尖括號,
    \1表示保留原有字符???  
  5. "hello".gsub(/./) {|s| s[0].
    to_s + ' '}
     #=> "104 101 108 108 111 "  

Ruby字符串處理函數(shù)字符串替換二:

 
 
 
  1. str.replace(other_str)
     => str  
  2. s = "hello" #=> 
    "hello"  
  3. s.replace "world"
     #=> "world" 

Ruby字符串處理函數(shù)6.字符串刪除:

 
 
 
  1. str.delete([other_str]+) 
    => new_str  
  2. "hello".delete "l","lo" 
    #=> "heo"  
  3. "hello".delete "lo" 
    #=> "he"  
  4. "hello".delete "aeiou", "^e" 
    #=> "hell"  
  5. "hello".delete "ej-m" 
    #=> "ho" 

Ruby字符串處理函數(shù)7.去掉前和后的空格

 
 
 
  1. str.lstrip =>
     new_str  
  2. " hello ".lstrip 
    #=> "hello "  
  3. "hello".lstrip
     #=> "hello" 

Ruby字符串處理函數(shù)8.字符串匹配

 
 
 
  1. str.match(pattern) =>
     matchdata or nil 

Ruby字符串處理函數(shù)9.字符串反轉

 
 
 
  1. str.reverse => new_str  
  2. "stressed".reverse 
    #=> "desserts" 

Ruby字符串處理函數(shù)10.去掉重復的字符

 
 
 
  1. str.squeeze([other_str]*) => new_str  
  2. "yellow moon".squeeze #=>
     "yelow mon" #默認去掉串中所有重復的字符  
  3. " now is the".squeeze(" ") #=>
     " now is the" #去掉串中重復的空格  
  4. "putters shoot balls".squeeze("m-z") 
    #=> "puters shot balls" 
    #去掉指定范圍內的重復字符 

Ruby字符串處理函數(shù)11.轉化成數(shù)字

 
 
 
  1. str.to_i=> str  
  2. "12345".to_i #=> 12345 

chomp和chop的區(qū)別:
chomp:去掉字符串末尾的\n或\r
chop:去掉字符串末尾的最后一個字符,不管是\n\r還是普通字符

 
 
 
  1. "hello".chomp #=> "hello"  
  2. "hello\n".chomp #=> "hello"  
  3.  
  4. "hello\r\n".chomp #=> "hello"  
  5. "hello\n\r".chomp #=> "hello\n"  
  6. "hello\r".chomp #=> "hello"  
  7. "hello".chomp("llo") #=> "he"  
  8.  
  9. "string\r\n".chop #=> "string"  
  10. "string\n\r".chop #=> "string\n"  
  11. "string\n".chop #=> "string"  
  12. "string".chop #=> "strin" 


網(wǎng)站標題:Ruby字符串處理函數(shù)總結列表分享
文章來源:http://www.dlmjj.cn/article/coeehhs.html