# 示例代码:去掉字符串中指定的子字符串
def remove_substring(original_str, sub_str_to_remove):
"""
去掉原字符串中所有出现的指定子字符串。
参数:
original_str (str): 原始字符串。
sub_str_to_remove (str): 需要去掉的子字符串。
返回:
str: 处理后的新字符串。
"""
return original_str.replace(sub_str_to_remove, "")
# 示例用法
original_string = "hello world"
substring_to_remove = "world"
new_string = remove_substring(original_string, substring_to_remove)
print(new_string) # 输出: "hello "
remove_substring
函数接收两个参数:original_str
是原始字符串,sub_str_to_remove
是需要去掉的子字符串。replace
方法将指定的子字符串替换为空字符串,从而实现去掉该子字符串的效果。上一篇:python中的set
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站