Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering Characters from a String [duplicate]

I need to make a function that takes two strings as imnput and returns a copy of str 1 with all characters from str2 removed.

First thing is to iterate over str1 with a for loop, then compare to str2, to accomplish subtraction I should create a 3rd string in which to store the output but I'm a little lost after that.

def filter_string(str1, str2):
    str3 = str1   
    for character in str1:
       if character in str2:
           str3 = str1 - str2
    return str3

This is what I've been playing with but I don't understand how I should proceed.

like image 854
SunshineTS Avatar asked Aug 11 '13 15:08

SunshineTS


People also ask

How do I filter text lines?

With this tool you can filter text lines. You can do two ways of filtering – the first way allows you to find and display all text lines that match contain the given search pattern. This pattern is simply a subtext of original text, which can be one or more characters, numbers, words or phrases. The second way is to use a regular expression.

What is a text filter tool?

Text filter tool What is a text filter? With this tool you can filter text lines. You can do two ways of filtering – the first way allows you to find and display all text lines that match contain the given search pattern. This pattern is simply a subtext of original text, which can be one or more characters, numbers, words or phrases.

How to add a character to an empty string in Java?

The indexOf () method returns -1 when the element is not present. So, when the string doesn't contain that character, we will add it to the empty string. Note: It maintains the insertion order of the characters in which they are added in the string. Let's implement the above steps in a Java program.

How to remove duplicate characters from a string in Python?

We will call the removeDuplicates () method and passed the string from which we need to remove duplicate characters. In the removeDuplicates () method, we will create a new empty string and calculate the original string's length.


2 Answers

Just use str.translate():

In [4]: 'abcdefabcd'.translate(None, 'acd')
Out[4]: 'befb'

From the documentation:

string.translate(s, table[, deletechars])

Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

If -- for educational purposes -- you'd like to code it up yourself, you could use something like:

''.join(c for c in str1 if c not in str2)
like image 198
NPE Avatar answered Oct 06 '22 00:10

NPE


Use replace:

def filter_string(str1, str2):
    for c in str2:
        str1 = str1.replace(c, '')
    return str1

Or a simple list comprehension:

''.join(c for c in str1 if c not in str2)
like image 24
lecodesportif Avatar answered Oct 05 '22 22:10

lecodesportif