views:

815

answers:

2

Hi,

I am trying to manipulate a string using Jython, I have included below an example string:

This would be a title for a website :: SiteName
This would be a title for a website :: SiteName :: SiteName

I am trying to remove all instances of ":: Sitename" or ":: SiteName :: SiteName", can anyone help me out? Cheers

+2  A: 

No different from regular 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 '
gimel
A: 

For such simple example it is unnecessary but in general you could use re module.

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))
J.F. Sebastian