本帖最后由 taroxd 于 2014-4-13 21:21 编辑
喵呜喵5 发表于 2014-4-13 20:43
谢谢,顺便问一下字符串有没有比较方便的删除某个位置字符的方法
比如 slice! 之类的?
=begin slice!(fixnum) → fixnum or nil slice!(fixnum, fixnum) → new_str or nil slice!(range) → new_str or nil slice!(regexp) → new_str or nil slice!(other_str) → new_str or nil Deletes the specified portion from str, and returns the portion deleted. =end string = "this is a string" string.slice!(2) #=> "i" string.slice!(3..6) #=> " is " string.slice!(/s.*t/) #=> "sa st" string.slice!("r") #=> "r" string #=> "thing"
=begin
slice!(fixnum) → fixnum or nil
slice!(fixnum, fixnum) → new_str or nil
slice!(range) → new_str or nil
slice!(regexp) → new_str or nil
slice!(other_str) → new_str or nil
Deletes the specified portion from str, and returns the portion deleted.
=end
string = "this is a string"
string.slice!(2) #=> "i"
string.slice!(3..6) #=> " is "
string.slice!(/s.*t/) #=> "sa st"
string.slice!("r") #=> "r"
string #=> "thing"
然后顶楼问题我没有重现,不知道是什么情况。我一般是这么写的
IO.foreach('test.txt') do |line| p line.chomp end
IO.foreach('test.txt') do |line|
p line.chomp
end
|