Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 搜索C++;字符串并删除文本(如果存在)_C++_String - Fatal编程技术网

C++ 搜索C++;字符串并删除文本(如果存在)

C++ 搜索C++;字符串并删除文本(如果存在),c++,string,C++,String,我必须处理两种类型的字符串: // get application name is simple function which returns application name. // This can be debug version or non debug version. So return value for this // function can be for eg "MyApp" or "MyApp_debug". string appl = getApplicationNa

我必须处理两种类型的字符串:

// get application name is simple function which returns application name. 
// This can be debug version or non debug version. So return value for this
// function can be for eg "MyApp" or "MyApp_debug".

string appl = getApplicationName();
appl.append("Info.conf");

cout << "Output of string is " << appl << endl;
//get application name是返回应用程序名称的简单函数。
//这可以是调试版本或非调试版本。所以返回这个值
//函数可以用于“MyApp”或“MyApp_调试”。
字符串appl=getApplicationName();
应用附加(“Info.conf”);
cout
string appl=getApplicationName()//MyAppInfo.conf或MyAppInfo_debug.conf。
size_t pos=appl.find(“_debug”);
if(pos!=字符串::npos)
应用=应用擦除(位置6);

cout我将包装
getApplicationName()
并调用包装器:

std::string getCanonicalApplicationName()
{
    const std::string debug_suffix = "_debug";
    std::string application_name = getApplicationName();
    size_t found = application_name.find(debug_suffix);
    if (found != std::string::npos)
    {
        application_name.replace(found, found + debug_suffix.size(), "");
    }
    return application_name;
}

请参阅和的文档。

当然,如果调试后缀从
\u debug
更改,您也需要更新硬编码的
6
。@Johnsyweb:True。但如果它改变了,那么您也必须修改解决方案中的
debug\u后缀
字符串。这就是我的观点。我的一个变化:你的两个变化。我认为在这种情况下使用erase()是正确的。
std::string getCanonicalApplicationName()
{
    const std::string debug_suffix = "_debug";
    std::string application_name = getApplicationName();
    size_t found = application_name.find(debug_suffix);
    if (found != std::string::npos)
    {
        application_name.replace(found, found + debug_suffix.size(), "");
    }
    return application_name;
}