954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Removing a substring from a string

Hi All,
I want to remove a substring from a string without any additional tabs/returns in the output string. Is there any method availaible or how can I do it. For the ease, I am giving an example:

mainstr ="""
${if:isLeaf}
  Dont include this isLeaf=True
${/if:isLeaf}

${if:isStatic}
  include this isStatic=True
${/if:isStatic}
"""


Now if I want to remove :

substr = ""
${if:isLeaf}
  Dont include this isLeaf=True
${/if:isLeaf}
"""


Expected output is :

${if:isStatic}
  include this isStatic=True
${/if:isStatic}


I am using mainstr.replace(substr, "") but it gives me additional carriage returns which leads to empty spaces as follows:

Actual Output:

${if:isStatic}
  include this isStatic=True
${/if:isStatic}


Note: See additional newlines before isStatic tag


Any Suggestions ?

ankit_rastogi82
Newbie Poster
7 posts since Aug 2005
Reputation Points: 14
Solved Threads: 0
 

This works just as expected ...
[php]mainstr ="""
${if:isLeaf}
Dont include this isLeaf=True
${/if:isLeaf}

${if:isStatic}
include this isStatic=True
${/if:isStatic}
"""

substr = """
${if:isLeaf}
Dont include this isLeaf=True
${/if:isLeaf}
"""

print mainstr.replace(substr, "")

""" result =

${if:isStatic}
include this isStatic=True
${/if:isStatic}
"""
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You