2008-10-23 71 views
0

我试图操纵用Jython字符串,我已经包含下面的示例串:Jython的劈裂串起

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

我想删除“:: Sitename”或“:: SiteName :: SiteName”的所有实例,任何人都可以帮我吗?干杯

回答

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 ' 
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))