ruby操作字符串

 
字符串是ruby最大的内建类,对于字符串的操作有75个以上的标准方法,下面介绍些使用频繁的方法:
1.分割!
例1:有如下文件
InBlock.gif/jazz/j00132.mp3    | 3:45|  Fats Waller        | Ain't Misbehavin' 
InBlock.gif/jazz/j00319.mp3    | 2:58|  Louis Armstrong    |Wonderful world 
InBlock.gif/bgrass/bg0732.mp3  | 4:09|  Strength  in Number | Texas Red
用对字符串的操作实现:
1.每行分割成各个字段;
2.把播放时间从mm:ss转换成秒;
3.删除歌曲演唱者名字中的多余空格
1.分割字段:string#split前面介绍过的方法,对于切割的模式可以用Regexp来匹配,/\s*\|\s*/传递给split,然后分割成字元
class Song 
  def initialize(title, name, duration) 
    @title=title 
    @name=name 
    @duration=duration 
  end 
  attr_reader :title, :name, :duration 
end 

class SongList 
  def initialize 
   @songs=Array. new   
  end 
   
  def [](index) 
     return  "#{@songs[index].name} #{@songs[index].title} #{@songs[index].duration}"    #这句不知道能不能有简单的方法实现?
  end 

   
  def append(song) 
   @songs.push(song)  
   self 
  end 
end 

f=File.open('test') 
songs=SongList. new 
f.each  do |line| 
  file, length, name, title= line.chomp.split(/\s*\|\s*/) 
  song=Song. new(name, title, length) 
  songs.append(song) 
end 
puts songs[1]
输出结果:Wonderful world Louis Armstrong 2:58
这不是跟结果体数组一样了么。。
2.有时候在名字保存的时候中间可能加入多余的空格(大于1个)可以用string#squeeze!(" ")方法把多个空格挤压成一个#squeeze是"挤压的意思"用!方法是改变字符串的方法。下面代码

#两个class类
f=File.open('test') 
songs=SongList. new 
f.each  do |line| 
  file, length, name, title= line.chomp.split(/\s*\|\s*/) 
   name.squeeze!(" ") 
  song=Song. new(name, title, length) 
  songs.append(song) 
end 
puts songs[1]
Wonderful world Louis Armstrong 2:58
转换时间格式:
在得出length以后可以对length再进行分割,分成 min和secs方法1:
min, secs=length.split(/:/)
duration=min.to_i*60+secs.to_i
方法2:
min, secs=length.scan(/\d+/)
duration=min.to_i*60+sec.to_i
用string#scan来扫描字符串中满足regexp的字元,这里regexp=/\d+/代表是数字的字元
输出结果
Wonderful world Louis Armstrong 178
 重写一下第一个步骤的代码:
InBlock.gif class Song
InBlock.gif  def initialize(format, name, title, duration)
InBlock.gif    @format=format
InBlock.gif    @name=name
InBlock.gif    @title=title
InBlock.gif    @duration=duration
InBlock.gif  end
InBlock.gif  
InBlock.gif  attr_accessor :format, :name, :title, :duration
InBlock.gif  
InBlock.gifend
InBlock.gif
class SongList
InBlock.gif  def initialize
InBlock.gif    @songs=Array. new
InBlock.gif  end
InBlock.gif  
InBlock.gif  def append(song)
InBlock.gif    @songs.push(song)
InBlock.gif    self
InBlock.gif  end
InBlock.gif  
InBlock.gif  def [](index)
InBlock.gif    @songs[index]
InBlock.gif  end
InBlock.gif  
InBlock.gif  def length
InBlock.gif    @songs.length
InBlock.gif  end
InBlock.gifend
InBlock.gif
f=File.open('test')
InBlock.gifsongs=SongList. new
InBlock.giff.each  do |line|
InBlock.gif  format, duration, name, title= line.split(/\s*\|\s*/)
InBlock.gif  song=Song. new(format, name, title, duration)
InBlock.gif  songs.append(song)
InBlock.gifend
InBlock.gif
(0..songs.length-1).each  do  |n|
InBlock.gif puts   "#{songs[n].format}--#{songs[n].name}--#{songs[n].duration}--#{songs[n].title}"
InBlock.gifend




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/64177,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值