ruby字串替換

2021-06-07 07:38:18 字數 470 閱讀 3414

第一種:"my name is #". #{}內是乙個ruby表示式, ruby解析表示式並呼叫返回值的to_s方法得到結果

第二種:here document 結構。

name="bob"

mail=welcome 

end最終mail裡的name表示式會被bob替代

第三種:c語言的printf-style字串

template="your name is %s."

puts template % 'bob' #=>your name is bob

puts template % 'john' #=>your name is john

第四種:erb模板,類似於jsp的樣子

require 'erb'

template = erb.new %q

food="bacon"

puts template.result(binding)

ruby分割字串 Ruby字串

構建方法 str hello world 只允許 與 轉義 str hello world 允許所有轉義和 字串拼接 str q hello world 等同單引號 str q 等同雙引號 str hello world eosstr abc 2 abcabc 索引str abc s str 1 s...

字串替換

描述輸入乙個字串,以回車結束 字串長度 100 該字串由若干個單詞組成,單詞之間用乙個空格隔開,所有單詞區分大小寫。現需要將其中的某個單詞替換成另乙個單詞,並輸出替換之後的字串。輸入輸入包括3行,第1行是包含多個單詞的字串 s,第2行是待替換的單詞a,長度 100 第3行是a將被替換的單詞b。長度 ...

字串替換

一 問題 函式宣告如下 char strreplace char str,char sub,char rep 其中str為原字串,sub為待被替換的子串。為簡單起見,假定字串sub和rep長度一樣 二 源 char strreplace char str,char sub,char rep if f...