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”获得中文帮助。
  1. ----------------------------------------------------------- String#split
  2.      str.split(pattern=$;, [limit])   => anArray

  3.      From Ruby 1.9.1
  4. ------------------------------------------------------------------------
  5.      Divides _str_ into substrings based on a delimiter, returning an
  6.      array of these substrings.

  7.      If _pattern_ is a +String+, then its contents are used as the
  8.      delimiter when splitting _str_. If _pattern_ is a single space,
  9.      _str_ is split on whitespace, with leading whitespace and runs of
  10.      contiguous whitespace characters ignored.

  11.      If _pattern_ is a +Regexp+, _str_ is divided where the pattern
  12.      matches. Whenever the pattern matches a zero-length string, _str_
  13.      is split into individual characters. If _pattern_ contains groups,
  14.      the respective matches will be returned in the array as well.

  15.      If _pattern_ is omitted, the value of +$;+ is used. If +$;+ is
  16.      +nil+ (which is the default), _str_ is split on whitespace as if `
  17.      ' were specified.

  18.      If the _limit_ parameter is omitted, trailing null fields are
  19.      suppressed. If _limit_ is a positive number, at most that number of
  20.      fields will be returned (if _limit_ is +1+, the entire string is
  21.      returned as the only entry in an array). If negative, there is no
  22.      limit to the number of fields returned, and trailing null fields
  23.      are not suppressed.

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

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


复制代码





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1