How to Remove a Substring from a Given String

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 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 want to change the prefix or suffix of a string and you're using Python 3.9 or newer, use str.removeprefix() or str.removesuffix() instead:

new_set = {x.removesuffix('.good').removesuffix('.bad') for x in set1}

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, "").

Remove a substring from a string in C

There is no predefined function in C to remove a given substring from a C string, but you can write one using strstr and memmove. Note that if you remove the substring in place, you cannot use memcpy nor strcpy because these have undefined behavior if the source and destination arrays overlap.

Here is the code:

#include <string.h>

char *strremove(char *str, const char *sub) {
size_t len = strlen(sub);
if (len > 0) {
char *p = str;
while ((p = strstr(p, sub)) != NULL) {
memmove(p, p + len, strlen(p + len) + 1);
}
}
return str;
}

Note that the resulting string may contain the substring as is the case in your example.

Netherwire suggested an optimisation:

char *strremove(char *str, const char *sub) {
size_t len = strlen(sub);
if (len > 0) {
char *p = str;
size_t size = 0;
while ((p = strstr(p, sub)) != NULL) {
size = (size == 0) ? (p - str) + strlen(p + len) + 1 : size - len;
memmove(p, p + len, size - (p - str));
}
}
return str;
}

Further honing the code, I came up with an even more efficient version using the 2 finger-method: only copying the fragments between matches starting after the first match:

char *strremove(char *str, const char *sub) {
char *p, *q, *r;
if (*sub && (q = r = strstr(str, sub)) != NULL) {
size_t len = strlen(sub);
while ((r = strstr(p = r + len, sub)) != NULL) {
memmove(q, p, r - p);
q += r - p;
}
memmove(q, p, strlen(p) + 1);
}
return str;
}

Here is the same method without any calls to memmove:

char *strremove(char *str, const char *sub) {
char *p, *q, *r;
if (*sub && (q = r = strstr(str, sub)) != NULL) {
size_t len = strlen(sub);
while ((r = strstr(p = r + len, sub)) != NULL) {
while (p < r)
*q++ = *p++;
}
while ((*q++ = *p++) != '\0')
continue;
}
return str;
}

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 of str 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;
}

How do I remove a substring from the end of a string?

strip doesn't mean "remove this substring". x.strip(y) treats y as a set of characters and strips any characters in that set from both ends of x.

On Python 3.9 and newer you can use the removeprefix and removesuffix methods to remove an entire substring from either side of the string:

url = 'abcdc.com'
url.removesuffix('.com') # Returns 'abcdc'
url.removeprefix('abcdc.') # Returns 'com'

The relevant Python Enhancement Proposal is PEP-616.

On Python 3.8 and older you can use endswith and slicing:

url = 'abcdc.com'
if url.endswith('.com'):
url = url[:-4]

Or a regular expression:

import re
url = 'abcdc.com'
url = re.sub('\.com$', '', url)

How to remove text from a string?

var ret = "data-123".replace('data-','');console.log(ret);   //prints: 123


Related Topics



Leave a reply



Submit