2016-04-26 131 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