#include <string.h>
#include <string>
#include <iostream>
// xxx.so --> xxx --> xxx_get_inst
int main()
{
std::string so_name = "xxx.so";
const char* dot_pos = strstr(so_name.c_str(), ".");
if (NULL == dot_pos)
{
return -1;
}
so_name[dot_pos - so_name.c_str()] = '\0';
std::string so_func = so_name + "_get_inst";
std::cout << so_func << std::endl; // xxx so_get_inst
std::cout << so_func.c_str() << std::endl; // xxx
return 0;
}
本意是想在 "." 的地方截断 "xxx.so" 得到 "xxx", 然后再拼接成 "xxx_get_inst".
结论:
1. 尽量避免直接修改 string 的内容;
2. append 或相加不等于 C 的字符串拼接