置換(replace、1つめだけ、ReplaceFirstOccurrence)
Python
py
s = s.replace('abc', 'ABC', 1) # 第2引数で数を指定
py
s = s.replace('abc', 'ABC', 1) # with string
s = re.sub('abc', 'ABC', s, 1) # with regexp
PHP
php
preg_replace('/xxx/', $after, $str)
str_replace()
の最後の引数に数字を入れるのは、置換した数が返ってくるだけで、回数を指定できるわけではないので注意
TypeScript
ts
s.replace("before", "after");
ts
s.replace("before", "after");
s.replace(/before/, "after"); // g をつけなければ1回だけ
Bash
bash
${str/before/after}
//
だと全部。 /
だと1個。
Ruby
rb
s = s.sub('abc', 'ABC') # with string
rb
s = s.sub('abc', 'ABC') # with string
s = s.sub(/abc/, 'ABC') # with regexp
s.sub!('abc', 'ABC') # destructive