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,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
SQL 安全 数据库
Ruby on Rails 数据库迁移操作深度解析
【7月更文挑战第19天】Rails 的数据库迁移功能是一个强大的工具,它帮助开发者以版本控制的方式管理数据库结构的变更。通过遵循最佳实践,并合理利用 Rails 提供的迁移命令和方法,我们可以更加高效、安全地管理数据库结构,确保应用的稳定性和可扩展性。
|
5月前
|
数据安全/隐私保护 Ruby 索引
|
5月前
|
存储 Ruby
|
5月前
|
Ruby
|
5月前
|
存储 Ruby
|
存储 Java 测试技术
Ruby 字符串从 1.8 到 2.5 的演变
Ruby 字符串从 1.8 到 2.5 的演变
Ruby 字符串从 1.8 到 2.5 的演变
|
索引 Ruby
【Ruby on Rails全栈课程】2.3 ruby的数据类型--字符串(String)、区间(Range)
1、字符串(String) 字符串是String类的对象。分为单引号字符串和双引号字符串。双引号字符串能支持较多的转义字符以及支持字符串#{}q嵌入变量。实际开发中多用双引号字符串。 (1)字符串嵌入变量用#{ },这个是ruby特有的,经常使用的一个功能。只支持双引号的字符串。
308 0
【Ruby on Rails全栈课程】2.3 ruby的数据类型--字符串(String)、区间(Range)