[英]How can I remove a phrase from a string but not the individual words
It is resolved , that was a lack of attention on my part.已经解决了,那是我的注意力不集中。
report = report.replace('contact sales', '')
works since replace
returns the new string and does not alter the original. report = report.replace('contact sales', '')
有效,因为replace
返回新字符串并且不改变原始字符串。
I want to remove some我想删除一些
contact sales
联系销售
instances from a string but I do not want to remove来自字符串的实例,但我不想删除
contact
接触
or要么
sales
销售量
if they exist individually.如果它们单独存在。
I tried: report.replace('contact sales', '')
but it did not work.我试过:
report.replace('contact sales', '')
但它没有用。 It detects the phrase but does not remove it.它会检测到该短语,但不会将其删除。 How do I get this done?
我如何完成这项工作?
You can get this done by redefining the variable when replacing instances.您可以通过在替换实例时重新定义变量来完成此操作。
For example:例如:
report = report.replace('contact sales', '')
The logic behind it is that the replace() function does not modify the string in place, it creates a new string with the replaced instances.其背后的逻辑是 replace() function 不会就地修改字符串,它会使用替换的实例创建一个新字符串。 That's why you need to redefine it.
这就是为什么你需要重新定义它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.