字符串是ruby最大的内建类,对于字符串的操作有75个以上的标准方法,下面介绍些使用频繁的方法:
1.分割!
例1:有如下文件
/jazz/j00132.mp3 | 3:45| Fats Waller | Ain't Misbehavin'
/jazz/j00319.mp3 | 2:58| Louis Armstrong |Wonderful world
/bgrass/bg0732.mp3 | 4:09| Strength in Number | Texas Red
/jazz/j00319.mp3 | 2:58| Louis Armstrong |Wonderful world
/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]
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]
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
重写一下第一个步骤的代码:
class Song
def initialize(format, name, title, duration)
@format=format
@name=name
@title=title
@duration=duration
end
attr_accessor :format, :name, :title, :duration
end
class SongList
def initialize
@songs=Array. new
end
def append(song)
@songs.push(song)
self
end
def [](index)
@songs[index]
end
def length
@songs.length
end
end
f=File.open('test')
songs=SongList. new
f.each do |line|
format, duration, name, title= line.split(/\s*\|\s*/)
song=Song. new(format, name, title, duration)
songs.append(song)
end
(0..songs.length-1).each do |n|
puts "#{songs[n].format}--#{songs[n].name}--#{songs[n].duration}--#{songs[n].title}"
end
def initialize(format, name, title, duration)
@format=format
@name=name
@title=title
@duration=duration
end
attr_accessor :format, :name, :title, :duration
end
class SongList
def initialize
@songs=Array. new
end
def append(song)
@songs.push(song)
self
end
def [](index)
@songs[index]
end
def length
@songs.length
end
end
f=File.open('test')
songs=SongList. new
f.each do |line|
format, duration, name, title= line.split(/\s*\|\s*/)
song=Song. new(format, name, title, duration)
songs.append(song)
end
(0..songs.length-1).each do |n|
puts "#{songs[n].format}--#{songs[n].name}--#{songs[n].duration}--#{songs[n].title}"
end
本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/64177,如需转载请自行联系原作者