給定每兩個整數的不同字串,在 Ruby (2.7) 中替換第二個整數的最簡單方法是什么?
例子:
'We are there from 2 to 5 oclock'
'You can stack 15 onto 22 boxes'
到目前為止我正在使用string::gsup,但我想知道是否有更好的方法?
uj5u.com熱心網友回復:
gsub是完成這項作業的最簡單方法。
輸入
a='We are there from 2 to 5 oclock'
代碼
p a.gsub(/\D*\d \D \K\d /,"10000")
輸出
"We are there from 2 to 10000 oclock"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/486277.html
標籤:红宝石
上一篇:ActiveRecordWhere子句在OR運算子中使用陣列項
下一篇:Ruby中的拆分和回圈
