我必须处理两种类型的字符串:
// 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;
上面的代码appl
是MyAppInfo.conf
或MyAppInfo_debug.conf
。
我的要求是它是调试还是非调试版本我应该只输出一个,即MyAppInfo.conf
。我们如何检查字符串中的_debug
是否存在以及如何删除它以便我们始终将输出字符串作为MyAppInfo.conf
?
答案 0 :(得分:1)
string appl = getApplicationName(); //MyAppInfo.conf or MyAppInfo_debug.conf.
size_t pos = appl.find("_debug");
if ( pos != string::npos )
appl = appl.erase(pos, 6);
cout << appl;
始终输出:
MyAppInfo.conf
参见示例输出:http://www.ideone.com/x6ZRN
答案 1 :(得分:1)
我会包装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;
}