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”);
coutstring 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;
}