赞 | 13 |
VIP | 118 |
好人卡 | 28 |
积分 | 12 |
经验 | 35779 |
最后登录 | 2017-7-6 |
在线时间 | 1564 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 1175
- 在线时间
- 1564 小时
- 注册时间
- 2008-7-30
- 帖子
- 4418
|
天啊!你干了一个究极严重的破坏。
String是一个类(常量),给他赋值将破坏掉String(对字串的处理),解决办法是换成小写的string。。。
split是String提供的方法(应该是你的单词拼写错误),提供一个参数或者正则表达式,按照参数或者正则表达式处理字符。
str2ary = "100,120,100,200,180".split(",")
p str2ary[1] #=> "120"
借用Ruby1.9.1提供的ri工具,列出了详尽的帮助。F1文档中搜索“split”获得中文帮助。- ----------------------------------------------------------- String#split
- str.split(pattern=$;, [limit]) => anArray
- From Ruby 1.9.1
- ------------------------------------------------------------------------
- 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.
- 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. If _pattern_ contains groups,
- the respective matches will be returned in the array as well.
- 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.
- 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.
- " 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", "", ""]
复制代码 |
评分
-
查看全部评分
|