自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Ruby字符串處理函數(shù)總結(jié)列表分享

開(kāi)發(fā) 開(kāi)發(fā)工具
Ruby字符串處理函數(shù)包括返回字符串長(zhǎng)度函數(shù);判斷字符串中是否包含另一個(gè)串函數(shù);字符串插入;字符串分隔,默認(rèn)分隔符為空格等等。

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

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

  1. str.length => integer 

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

  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.字符串分隔,默認(rèn)分隔符為空格

  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*" #將元音替換成*號(hào)  
  4. "hello".gsub(/([aeiou])/, '<\1>') 
    #=
    > "h<e>ll<o>" #將元音加上尖括號(hào),
    \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.字符串反轉(zhuǎn)

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

 

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

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

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

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

chomp和chop的區(qū)別:
chomp:去掉字符串末尾的\n或\r
chop:去掉字符串末尾的最后一個(gè)字符,不管是\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" 

責(zé)任編輯:曹凱 來(lái)源: chinaunix.net
相關(guān)推薦

2009-11-17 10:55:02

PHP字符串處理函數(shù)

2009-12-15 13:15:11

Ruby字符串

2009-11-24 09:55:44

PHP字符串函數(shù)

2010-10-09 11:54:46

MySQL字符串

2010-07-14 16:35:52

Perl字符串處理函數(shù)

2009-12-14 13:19:07

Ruby字符串

2009-12-01 10:38:08

PHP字符串函數(shù)

2010-07-19 15:07:46

Perl字符串處理函數(shù)

2010-10-09 12:01:00

MySQL字符串

2010-11-26 11:20:31

MySQL字符串處理函

2009-12-17 13:23:25

Ruby eval方法

2010-11-26 09:51:54

MySQL字符串

2011-07-12 16:14:17

2014-01-02 16:14:10

PostgreSQL字符串

2023-08-26 20:21:58

字符KotlinJava

2023-08-21 10:28:00

字符串字符Python

2009-12-11 13:16:04

PHP查詢字符串

2022-05-18 11:35:17

Python字符串

2010-02-04 10:52:36

C++字符串分割函數(shù)

2010-09-09 11:48:00

SQL函數(shù)字符串
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)