查看: 2210|回复: 3

[已经过期] 关于截取字符串的代码,谁能帮我解决下bug

[复制链接]
梦石
0
星屑
1907
在线时间
1555 小时
注册时间
2013-4-13
回帖
783
发表于 2013-4-16 14:40:44 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 沉滞的剑 于 2013-4-16 16:06 编辑

[pre lang="ruby" line="1"]$perks = []
class Perk
  attr_accessor :index
  attr_accessor :name
  attr_accessor :note
  attr_accessor :fatherid
  attr_accessor :description
##=============================================================================
def finding (tag)
   a = @note.index(tag)
   a = a + tag.size unless a == nil
   b = @note.index("!",a-1) unless a == nil
   c = @note[a..b-1].to_i unless a == nil or b == nil
   return c
end
  def finding_s (tag)
    a = @note.index(tag)
    p a = a + tag.size unless a == nil
    p b = @note.index("!",a+1) unless a == nil
    p c = @note[a,b].to_s unless a = nil or b == nil
   return c
end
##=============================================================================
  def initialize (index,name,note)
    @index = index
    @name = name
    p @note = note
    @fatherid = finding("<f>")
    @description = finding_s("<d>")
  end
  def self.setup
  for i in $data_skills
    if i != nil
      b = i.name
      a = i.id
      c = i.note
      $perks[a]= Perk.new(a,b,c)
    end  
  end
end
end
[/pre]

1.jpg
梦石
0
星屑
55
在线时间
42 小时
注册时间
2009-12-2
回帖
25
发表于 2013-4-16 15:11:44 | 显示全部楼层
s = "12321"
p s[1,3]

正则表达式只是用来匹配特定形式的字符串,跟你说的截取无关,具体看vx版帮助吧

点评

截取字符串我成功了...用的是[a..b],不过编译器还是不放我过去.....已经更新代码  发表于 2013-4-16 15:24
回复

使用道具 举报

梦石
0
星屑
55
在线时间
323 小时
注册时间
2010-8-21
回帖
638
发表于 2013-4-16 18:06:19 | 显示全部楼层
用正则表达式吧..这头都大了{:2_270:}

@note[/*\[f:(\d+)\]*/]可以匹配[f:5]
就是fatherid 可以设为 5了
  1. if @note[/*\[f:(\d+)\]*/]
  2.   @fatherid = $1.to_i
  3. else
  4.   # 匹配不成功时
  5. end
复制代码
最重要的是这支持中文,截取字节的话,中文很麻烦

点评

看不懂......正则表达式的语法我一直就搞不明白....  发表于 2013-4-17 00:17

评分

参与人数 1星屑 +30 收起 理由
j433463 + 30 我很赞同

查看全部评分

>>猛戳>>MetalSagaR游戏主页<<这里<<
欢迎提供您的意见
回复

使用道具 举报

梦石
0
星屑
50
在线时间
466 小时
注册时间
2013-2-8
回帖
420
发表于 2013-4-18 16:18:06 | 显示全部楼层
本帖最后由 SuperMario 于 2013-4-18 16:55 编辑

@沉滞的剑 ,没注意到你还需要读取数据~
那还是使用正则表达式更方便,这块我也不太清楚,我一般是这样用:
  1. /(f:)(\d+)/ =~ "d:13 f:12 e:"
  2. p $1 # "f:" 第一次匹配成功的值
  3. p $2 # "12" 第二次
复制代码
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部