943,985 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 17246
  • Python RSS
Jan 16th, 2006
0

Removing a substring from a string

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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 :
Python Syntax (Toggle Plain Text)
  1. substr = ""
  2. ${if:isLeaf}
  3. Dont include this isLeaf=True
  4. ${/if:isLeaf}
  5. """
  6.  

Expected output is :
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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 ?
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
ankit_rastogi82 is offline Offline
7 posts
since Aug 2005
Jan 17th, 2006
0

Re: Removing a substring from a string

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]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Limiting the size of List
Next Thread in Python Forum Timeline: Best way to convert C++ coding to Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC