Removing a substring from a string

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 7
Reputation: ankit_rastogi82 is an unknown quantity at this point 
Solved Threads: 0
ankit_rastogi82 ankit_rastogi82 is offline Offline
Newbie Poster

Removing a substring from a string

 
0
  #1
Jan 16th, 2006
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:

  1. mainstr ="""
  2. ${if:isLeaf}
  3. Dont include this isLeaf=True
  4. ${/if:isLeaf}
  5.  
  6. ${if:isStatic}
  7. include this isStatic=True
  8. ${/if:isStatic}
  9. """

Now if I want to remove :
  1. substr = ""
  2. ${if:isLeaf}
  3. Dont include this isLeaf=True
  4. ${/if:isLeaf}
  5. """
  6.  

Expected output is :
  1. ${if:isStatic}
  2. include this isStatic=True
  3. ${/if:isStatic}

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

Actual Output:
  1.  
  2.  
  3.  
  4.  
  5.  
  6. ${if:isStatic}
  7. include this isStatic=True
  8. ${/if:isStatic}

Note: See additional newlines before isStatic tag


Any Suggestions ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Removing a substring from a string

 
0
  #2
Jan 17th, 2006
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC