我是 C++ 的新手,这就是我需要一些帮助的原因。
在 Python 中我会这样做:
myString = "test|arg"
myArg = myString.split("|")[0]
但现在我在使用 C++,我有一个字符,想删除一个特殊的部分。
#include "stdafx.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
bool isPartOf(const std::string& word, const std::string& sentence) {
return sentence.find(word) != std::string::npos;
}
int _tmain(int argc, _TCHAR* argv[])
{
char* myChar= "test|me";
if (isPartOf("|me", myChar)) {
std::string sStr(myChar);
boost::replace_all(sStr, "|me", "");
std::copy(sStr.begin(), sStr.end(), myChar);
myChar[sStr.size()] = '\0';
printf(myChar);
system("pause>nul");
}
}
这是我当前的代码,但我收到此错误:
Error 1 error C4996: 'std::_Copy_impl': Function call with parameters that >may be unsafe - this call relies on the caller to check that the passed values >are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See >documentation on how to use Visual C++ 'Checked Iterators' c:\program files >(x86)\vc\include\xutility 2132 1 ConsoleApplication2
我希望有人能帮助我
最好的问候
最佳答案
C++ 相当于你的 Python 程序:
#include <string>
#include <iostream>
int main(void) {
std::string myString = "test|me";
std::string myArg = myString.substr(0, myString.find('|'));
std::cout << myArg << std::endl;
}
输出
test
关于C++从char中删除子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36859620/