2016-04-26 135 views
0

我对C++很新颖,这就是为什么我需要一些帮助。C++从char中删除子字符串

在Python我会做这种方式:

myString = "test|arg" 
myArg = myString.split("|")[0] 

但现在我在C++中,我有一个char,想有点删除一个特殊的部分。

#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

我希望有人能帮助我

问候

+0

在C++中,你也许会使用'的std :: string'代替'字符*' – user463035818

+0

您的代码重写一个字符串。你不能这样做。 – davmac

回答

2

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