程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA贊助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完全版整頓

JAVA贊助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完全版整頓

編輯:關於JAVA

JAVA贊助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完全版整頓。本站提示廣大學習愛好者:(JAVA贊助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完全版整頓)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA贊助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完全版整頓正文


1.前往字符串的長度

str.length => integer

2.斷定字符串中能否包括另外一個串

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

3.字符串拔出

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

4.字符串分隔,默許分隔符為空格

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

5.字符串調換

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

字符串調換二:

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

6.字符串刪除

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

7.去失落前和後的空格

str.lstrip => new_str
" hello ".lstrip   #=> "hello "
"hello".lstrip       #=> "hello"

8.字符串婚配

str.match(pattern) => matchdata or nil

9.字符串反轉

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

10.去失落反復的字符

str.squeeze([other_str]*) => new_str
"yellow moon".squeeze                  #=> "yelow mon" #默許去失落串中一切反復的字符
" now   is the".squeeze(" ")         #=> " now is the" #去失落串中反復的空格
"putters shoot balls".squeeze("m-z")   #=> "puters shot balls" #去失落指定規模內的反復字符

11.轉化成數字

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

chomp和chop的差別:

chomp:去失落字符串末尾的\n或\r
chop:去失落字符串末尾的最初一個字符,不論是\n\r照樣通俗字符


"hello".chomp            #=> "hello"
"hello\n".chomp          #=> "hello"
"hello\r\n".chomp        #=> "hello"
"hello\n\r".chomp        #=> "hello\n"
"hello\r".chomp          #=> "hello"
"hello".chomp("llo")     #=> "he"

"string\r\n".chop   #=> "string"
"string\n\r".chop   #=> "string\n"
"string\n".chop     #=> "string"
"string".chop       #=> "strin"

split是String類的一個類辦法,我依據ri String.split供給的內容簡略翻譯一下。
----------------------------------------------------------- String#split
str.split(pattern=$;, [limit]) => anArray
------------------------------------------------------------------------
Divides _str_ into substrings based on a delimiter, returning an
array of these substrings.
將一個字符串用分隔符朋分成一些子字符串,並前往一個包括這些子字符串的數組。

If _pattern_ is a +String+, then its contents are used as the
delimiter when splitting _str_. If _pattern_ is a single space,
_str_ is split on whitespace, with leading whitespace and runs of
contiguous whitespace characters ignored.
假如pattern部門是一個字符串,那末用它作朋分符來分隔,假如pattern是一個空格,那末在空格處罰割,而且鄰近的空格被疏忽。

If _pattern_ is a +Regexp+, _str_ is divided where the pattern
matches. Whenever the pattern matches a zero-length string, _str_
is split into individual characters.
假如pattern是個正則表達式,那末在婚配pattern的處所朋分,當pattern是長度為0的字符串,那末split將把字符串朋分為單個字符

If _pattern_ is omitted, the value of +$;+ is used. If +$;+ is
+nil+ (which is the default), _str_ is split on whitespace as if `
' were specified.
假如pattern被疏忽,將用$;來分隔,假如$;沒有設置(就是在默許狀況),split將制訂空格' '
If the _limit_ parameter is omitted, trailing null fields are
suppressed. If _limit_ is a positive number, at most that number of
fields will be returned (if _limit_ is +1+, the entire string is
returned as the only entry in an array). If negative, there is no
limit to the number of fields returned, and trailing null fields
are not suppressed.
假如limit參數被疏忽,跟蹤空段被克制,假如limit是個負數,那末至少前往limit個字段(假如是1,那末將全部字符串作為一個字段前往),假如是個正數,那末跟蹤空段不被克制。

" now's the time".split #=> ["now's", "the", "time"]
" now's the time".split(' ') #=> ["now's", "the", "time"]
" now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
"hello".split(//) #=> ["h", "e", "l", "l", "o"]
"hello".split(//, 3) #=> ["h", "e", "llo"]
"hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]

"mellow yellow".split("ello") #=> ["m", "w y", "w"]
"1,2,,3,4,,".split(' ,') #=> ["1", "2", "", "3", "4"]
"1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
"1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]

假如包括特別字符,留意本義
"wo | shi | yi | ge | bing".split(/\s*\|\s*) #豎槓別忘了本義

還有它和String.scan的差別,split中的pattern是分隔符,而scan中的pattern指的是要婚配的器械。

"123=342=4234=523421=6424".scan(/\d+/) #=> ["123","342","4234","523421","6424"]


假如婚配項被括起來,那末則會保存朋分符,例如:

"Three little words".split(/\s+/) #===>["three","little",words"]
"Three little words".split(/(\s+)/) #===>["three"," ","little"," ","words"] 保存了空格

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved