我正在尝试创建一种从字符串中删除子字符串的方法。它是 char 形式的,所以我不能使用方便的方法来为字符串高兴地提供。我知道如何搜索它,我可以找到索引,但删除 char 对我来说有点冒险。如果有人有任何想法,我将不胜感激。谢谢。
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;
char str[20];
char del[20];
char * delStorage;
char select(char);
int main()
{
cout << "Enter a string to the console." << endl;
cin >> str;
cout << "You inputted " << str << " for the string." << endl;
select(letter);
return 0;
}
char select(char letter)
{
cout << "Enter one of the following commands d for delete." << endl;
cin >> letter;
switch (letter)
{
case 'D':
case 'd':
cout << "Enter a string to delete." << endl;
cin >> del;
delStorage = strstr(str, del);
if (delStorage)
{
cout << del << " has been found. Deleting..." << endl;
delStorage.Replace(str, del, "");
}
break;
}
我不能使用 Replace 方法,因为它是字符。char
我已经尝试过嵌套循环,但由于and的不兼容而被卡住了int
。如果有人有建议,我会全力以赴。再次感谢 Stack 社区。