C++从char中删除子字符串

标签 c++ replace char substring

我是 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/

相关文章:

c++ - 使用字符串替换函数用初始化变量替换字符串的一部分?

javascript - 将零添加到非前导零日期时间字符串正则表达式

java - 验证输入的 char 变量。 do-while 循环不会中断

c - Qsort 不排序 char * 数组

java - 将 char 数组转换为字节数组并再次返回

c++ - 我的 shared_ptr 初始化列表代码有什么问题?

c++ - 在 Gaia 支持下构建音频分析库 Essentia 失败

c++ - SFINAE 显式工作但不隐式工作

c++ - 如何将 lambda 函数与 std::replace_if() 一起使用?

c++ - 远程进程不启动