473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Removing substring from string

I am trying to remove a specific substring from a string... Here are the doctests that are supposed to pass. I'm just absolutely stumped. Can someone point me in the right direction on where to start?

def remove(sub, s):

"""
>>> remove('an', 'banana')
'bana'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


def remove_all(sub, s):
"""
>>> remove('an', 'banana')
'ba'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


if __name__ == '__main__':
import doctest
doctest.testmod()
Nov 2 '09 #1
2 99681
bvdet
2,851 Expert Mod 2GB
I don't understand the question. Surely it's not as simple as this:
Expand|Select|Wrap|Line Numbers
  1. >>> 'banana'.replace('an', '')
  2. 'ba'
  3. >>> 'banana'.replace('an', '', 1)
  4. 'bana'
  5. >>> 'Mississippi'.replace('iss', '')
  6. 'Mippi'
  7. >>> 'Mississippi'.replace('iss', '', 1)
  8. 'Missippi'
  9. >>> 
Nov 2 '09 #2
This is one solution for remove_all:
Expand|Select|Wrap|Line Numbers
  1. import string
  2.  
  3. def remove_all(substr, str):
  4.     index = 0
  5.     length = len(substr)
  6.     while string.find(str, substr) != -1:
  7.         index = string.find(str, substr)
  8.         str = str[0:index] + str[index+length:]
  9.     return str
  10.  
Jan 24 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: becte | last post by:
I am little bit confused Is this a legal way of removing a substring from a string? What about the second alternative using strcpy, is it ok even though the source and dest. strings overlap? ...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
5
by: Isa Janfada | last post by:
Hello, I have a html text string like this: " When  creating  a  new  message,  Reset  occurs  when  '\'  is  entered  in ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: jobs | last post by:
I have a gridview with data that's a link to another page. I want to pass search information to the called page, but I don't want that query string to remain in the address line beyond the...
6
by: lawpoop | last post by:
Hello all - I have a situation where I want to remove a string from a file and write it to disk before I do further work with it. It's a malformed xlm document with a string that causes our...
4
by: Jack | last post by:
I am fairly new to c++. I am trying to write a function that removes all occurrences of one string from another without using any C library functions. So here is the function prototype: void...
4
by: Ahmed, Shakir | last post by:
I need to remove text string from the list of the numbers mentioned below: 080829-7_A 070529-5_c 080824-7_O 070405_6_p The output will be : 080829-7 070529-5
0
by: Adam Pletcher | last post by:
You just want to drop the last two characters? Slice it. 080829-7 - Adam Behalf
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.