#include <iostream>
using namespace std;
int remove_last_substr(std::string& main_str, const std::string& sub_str) {
size_t pos = main_str.rfind(sub_str);
if (pos != std::string::npos) {
main_str.erase(pos, sub_str.length());
}
return 0;
}
int remove_url_parameter(std::string& url, const std::string& param_name) {
std::string::size_type param_start_pos = url.rfind("?");
if (param_start_pos == std::string::npos) {
return 0;
}
std::string::size_type param_pos = url.find("?" + param_name + "=", param_start_pos);
cout << "param_pos1:" << param_pos << endl;
if (param_pos == std::string::npos) {
param_pos = url.find("&" + param_name + "=", param_start_pos);
}
cout << "param_pos2:" << param_pos << endl;
if (param_pos != std::string::npos) {
std::string::size_type param_end_pos = url.find('&', param_pos + 1);
cout << "param_end_pos2:" << param_end_pos << endl;
if (param_end_pos != std::string::npos) {
url.erase(param_pos + 1, param_end_pos - param_pos);
} else {
url.erase(param_pos);
}
}
return 0;
}
int remove_url_parameter2(std::string& url, const std::string& param_name) {
std::vector<std::string> params;
boost::split(params, url, boost::algorithm::is_any_of("&"));
std::vector<std::string> left_params;
int param_index = 0;
for (std::string& param : params) {
std::vector<std::string> kv;
std::string
param_index++;
boost::split(kv, param, boost::algorithm::is_any_of("="));
if (kv.size() != 2) {
continue;
}
if (param_index == 0) {
std::vector<std::string> prefix_param;
boost::split(prefix_param, kv[0], boost::algorithm::is_any_of("?"));
if (prefix_param.size() < 2) {
continue;
}
}
if (kv[0] == param_name) {
continue;
}
left_params.push_back(param);
}
return 0;
}
int main() {
string a = "xc=1?cc=2c=3&cc=4&c=&b=2&c=6&c=7";
remove_url_parameter(a, "c");
cout << a << endl;
return 0;
}