str_replace() 對字符串進(jìn)行批量或多次替換,類似PHP的str_replace()。
str_replace() 接收三個(gè)參數(shù),第一個(gè)為要替換的字符串,第二個(gè)為要查找的字符串,第三個(gè)為替換后的新字符串。
以下是 str_replace() 的語法:
str_replace(find, old=[], new=None)
● find [str]:要替換的字符串。
● old [list]:要查找的字符串,必須是列表。
● new [list|str]:替換后的新字符串,可以是列表或字符。
返回替換后的字符串。
以下展示了使用 str_replace() 的實(shí)例:
from kyger.utility import str_replace print(str_replace('https://www.kg.cn', ['kg', 'cn'], ['kgcms', 'com'])) # 多換多 print(str_replace('[{"AGE": "15"}{"Age": "18"}]', ['AGE', 'Age'], 'age')) # 一換多
以上實(shí)例運(yùn)行后輸出的結(jié)果為:
'http://cnkunpeng.cn' '[{"age": "15"}{"age": "18"}]'