查看: 2270|回复: 2

[已经解决] 杂样把这个东东变成数组

[复制链接]
梦石
0
星屑
1547
在线时间
688 小时
注册时间
2009-11-11
回帖
2692
发表于 2010-10-11 22:10:44 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 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类的用法

请脚本高人指点指点~   谢谢

点评

e,对RUBY还不熟悉,以前只知道点vb的说  发表于 2010-10-13 11:00
String.class #=> Class 这已经不只是简简单单是不是常量的问题了。。。  发表于 2010-10-12 12:42

嘿。嘿。嘿

万物创造者

梦石
0
星屑
54
在线时间
352 小时
注册时间
2008-2-15
回帖
2406
发表于 2010-10-11 22:35:05 | 显示全部楼层
用split匹配正则吧

点评

能具体写一下吗 把这个转成数组 String="100,120,100,200,180" VX在家里....  发表于 2010-10-11 22:46
From mortal hope immortal power springs.
回复

使用道具 举报

梦石
0
星屑
1580
在线时间
1564 小时
注册时间
2008-7-30
回帖
4207

贵宾

发表于 2010-10-11 22:53:38 | 显示全部楼层
天啊!你干了一个究极严重的破坏。
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", "", ""]


复制代码

点评

恩,呵呵,我这里只是举例,里面我当然还是小写,WLH还是Wlh只要一个字大写就是常量,谢谢你详细的回答,我也刚搜答案 [url]http://www.phpfans.net/manu/ruby/18.html[/url]   发表于 2010-10-11 23:04

评分

参与人数 1星屑 +700 收起 理由
夕阳武士 + 700 认可答案

查看全部评分


See FScript Here:https://github.com/DeathKing/fscript
潜心编写URG3中。
所有对URG3的疑问和勘误或者建议,请移步至发布页面。
欢迎萌妹纸催更
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部