Jython Splitting String Up

时间:2008-10-23 15:12:04

标签: string jython

我正在尝试使用Jython操作一个字符串,我在下面包含了一个示例字符串:

这将是网站:: SiteName的标题 这将是一个网站的标题:: SiteName :: SiteName

如何删除“:: Sitename”或“:: SiteName :: SiteName”的所有实例?

2 个答案:

答案 0 :(得分:2)

与常规Python没有什么不同:

>>> str="This would be a title for a website :: SiteName"
>>> str.replace(":: SiteName","")
'This would be a title for a website '
>>> str="This would be a title for a website :: SiteName :: SiteName"
>>> str.replace(":: SiteName","")
'This would be a title for a website '

答案 1 :(得分:0)

对于这样一个简单的例子,这是不必要的,但一般来说你可以使用re模块。

import re

sitename = "sitename" #NOTE: case-insensitive
for s in ("This would be a title for a website :: SiteName :: SiteName",
          "This would be a title for a website :: SiteName"):
    print(re.sub(r"(?i)\s*::\s*%s\s*" % sitename, "", s))