How can I remove a substring from a given String?
You could easily use String.replace()
:
String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");
How to remove all substrings from a string
Removes all instances of the pattern from a string,
#include <string>
#include <iostream>
using namespace std;
void removeSubstrs(string& s, string& p) {
string::size_type n = p.length();
for (string::size_type i = s.find(p);
i != string::npos;
i = s.find(p))
s.erase(i, n);
}
int main() {
string str = "red tuna, blue tuna, black tuna, one tuna";
string pattern = "tuna";
removeSubstrs(str, pattern);
cout << str << endl;
}
How to remove specific substrings from a set of strings in Python?
Strings are immutable. str.replace
creates a new string. This is stated in the documentation:
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. [...]
This means you have to re-allocate the set or re-populate it (re-allocating is easier with a set comprehension):
new_set = {x.replace('.good', '').replace('.bad', '') for x in set1}
P.S. if you're using Python 3.9 or newer, see DineshKumar answer.
Most efficient way to remove multiple substrings from string?
Regex:
>>> import re
>>> re.sub(r'|'.join(map(re.escape, replace_list)), '', words)
' word2 word4, '
The above one-liner is actually not as fast as your string.replace
version, but definitely shorter:
>>> words = ' '.join([hashlib.sha1(str(random.random())).hexdigest()[:10] for _ in xrange(10000)])
>>> replace_list = words.split()[:1000]
>>> random.shuffle(replace_list)
>>> %timeit remove_multiple_strings(words, replace_list)
10 loops, best of 3: 49.4 ms per loop
>>> %timeit re.sub(r'|'.join(map(re.escape, replace_list)), '', words)
1 loops, best of 3: 623 ms per loop
Gosh! Almost 12x slower.
But can we improve it? Yes.
As we are only concerned with words what we can do is simply filter out words from the words
string using \w+
and compare it against a set of replace_list
(yes an actual set
: set(replace_list)
):
>>> def sub(m):
return '' if m.group() in s else m.group()
>>> %%timeit
s = set(replace_list)
re.sub(r'\w+', sub, words)
...
100 loops, best of 3: 7.8 ms per loop
For even larger string and words the string.replace
approach and my first solution will end up taking quadratic time, but the solution should run in linear time.
How do I remove a list of substrings from a given string in Python?
You could just use str.replace()
to replace the substrings with ""
. This also means that the final result would need to be split and joined by " "
to only have one whitespace between the words after replacing. You can use str.split()
and str.join()
for this.
string = "Play soccer tomorrow from 2pm to 3pm @homies"
times = ["tomorrow", "from 2pm to 3pm"]
for time in times:
string = string.replace(time, "")
print(" ".join(string.split()))
# Play soccer @homies
Note: Strings are immutable in python, so you cannot simply modify it in-place with string.replace(time, "")
. You need to reassign the string with string = string.replace(time, "")
.
removing all sub strings of a string in C
Your code has multiple issues:
the loop
for (int i = 0; i <= strlen(str); i++)
is very inefficient as the length ofstr
is recomputed at each iteration of the loop. You should simply write:for (int i = 0; str[i] != '\0'; i++)
when you skip the matched substring, the character after it is copied and
i
is incremented in the loop so the next occurrence is not tested correctly.the code to append a single character is inefficient: instead of
strncat(new, &ch, 1);
you should have a pointer to the end of the destination string and write*p++ = str[i];
and set the null terminator after the end of the loop.
Here is a modified version:
#include <string.h>
// remove all occurrences of sub in str into destination buffer new
// return a pointer to the destination string
char *removeSub(cont char *str, const char *sub, char *new) {
char *p = new;
size_t len = strlen(sub);
if (len > 0) {
const char *subp;
while ((subp = strstr(str, sub)) != NULL) {
memcpy(p, str, sub - str);
p += sub - str;
str = sub + len;
}
}
strcpy(p, str);
return new;
}
And an alternative without library calls:
// remove all occurrences of sub in str into destination buffer new
// return a pointer to the destination string
char *removeSub(cont char *str, const char *sub, char *new) {
char *p = new;
while (*str) {
if (*str == *sub) { /* potential substring match */
for (size_t i = 1;; i++) {
if (sub[i] == '\0') { /* substring match */
str += i;
break;
}
if (str[i] != sub[i]) {
*p++ = *str++;
break;
}
}
} else {
*p++ = *str++;
}
}
*p = '\0';
return new;
}
Remove multiple substring from a string Python
Shortly with regexp substitution:
import re
my_str = "bad-Classname's"
my_str = re.sub(r"[ ,/'’-]", "", my_str)
print(my_str) # badClassnames
[ ,/'’-]
- regex character class, matches a single character in the list ",/'’-
"
remove all sub-strings from a string
Try this:
def replace(mystring, combinations):
val = next((val for val in combinations if val in mystring), None)
while val is not None:
mystring = mystring.replace(val, '')
val = next((val for val in combinations if val in mystring), None)
return mystring
Basically you find the first combination that can be found in mystring
(this can be done with next((val for val in combinations if val in mystring), None)
). If no such a combination can be found then val
will be None
.
Then you replace that specific combination with ''
. And you repeat. You stop when such combination cannot be found anymore (i.e., when val
is None
).
Examples:
>>> replace('ZXYUYY', ['XY', 'ZU', 'YY'])
''
>>> replace('ZXYUYY', ['XY', 'YY'])
'ZU'
>>> replace('AZXYUYY', ['XY', 'ZU', 'YY'])
'A'
>>> replace('AZBXYUYY', ['XY', 'ZU', 'YY'])
'AZBU'
Related Topics
Any Solution to Unpack a Vector to Function Arguments in C++
Is Global Memory Initialized in C++
C++ Static Template Member, One Instance for Each Template Type
Write Concurrently Vector<Bool>
A Class Name Introduced Inside a Class Is Not Treated as a Nested Class Name
C++ & Boost: Encode/Decode Utf-8
Function Composition in C++/C++11
Compile-Time or Runtime Detection Within a Constexpr Function
Forwarding All Constructors in C++0X
C++ Automatic Factory Registration of Derived Types
Conflict Between Dynamic Linking Priority in Osx
C++ Template Functions Overload Resolution
C++ Type Registration at Compile Time Trick
Subtle C++ Inheritance Error with Protected Fields
How to Use C++20 Modules with Cmake
How to Automatically Avoiding Stepping into Certain Functions in Visual Studio