[英]Ruby substring downcase
是否有更合適的方法來使一個子字符串比這個更簡單?:
string[a..b] = string[a..b].downcase
也許是這樣的:
string[a..b].downcase!
例如:
string = "RubyChangedMyMind"
string[1..string.length] = string[1..string.length].downcase
# this is working but i want to know if there is more short way to do this
# (indeces may be different)
# something like string[a..b].downcase!
你可以做proc:
my_downcase = Proc.new { |str,n,m| str.chars.each_with_index.map{ |c,index| (n..m).include?(index) ? c.downcase : c }.join}
並隨時調用它:
my_downcase.call(string,range_from,range_to)
例如:
string = "dsadaDAdasdsadDADADadsadADD"
new_string = my_downcase.call(string,0,11)
#=> "dsadadadasdsadDADADadsadADD"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.