c++ string -=
时间: 2023-10-28 20:17:09 浏览: 95
c++ string
There is no such operator as `string -=` in C. The `-=` operator is used for arithmetic operations on numeric variables, but it cannot be used with strings.
If you want to remove a substring from a C string, you need to use string manipulation functions such as `strstr()` and `strcpy()`. For example, you can use `strstr()` to find the position of the substring you want to remove, and then use `strcpy()` to copy the remaining part of the string to a new location.
Here's an example code snippet that removes a substring from a C string:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello world!";
char substr[] = "world";
char* pos = strstr(str, substr); // Find the position of the substring
if (pos != NULL) {
strcpy(pos, pos + strlen(substr)); // Copy the remaining part of the string
}
printf("%s\n", str); // Output: "Hello !"
return 0;
}
```
阅读全文