Project1
标题:
杂样把这个东东变成数组
[打印本页]
作者:
summer92
时间:
2010-10-11 22:10
标题:
杂样把这个东东变成数组
本帖最后由 summer92 于 2010-10-11 23:05 编辑
String="100,120,100,200,180"
data=[]
data=String.splet(",")
p data[0] ==> "100"
p data[4] ==> "180"
--------------------------------------------------
理论上是这样,可是我翻遍了F1,楞是没找到 String.splet(",") 的类似的String类的用法
请脚本高人指点指点~ 谢谢
作者:
小幽的马甲
时间:
2010-10-11 22:35
用split匹配正则吧
作者:
DeathKing
时间:
2010-10-11 22:53
天啊!你干了一个究极严重的破坏。
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", "", ""]
复制代码
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1