我正在寻找一种准备用作URL的字符串的方法。
代码的基础是您键入要查找的内容,并使用您键入的内容打开浏览器。我正在学习C ++,因此这是一个学习程序。并且请尽可能具体,因为我是C ++的新手。
这是我正在尝试做的事情:
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input.c_str());
Run Code Online (Sandbox Code Playgroud)
但是我试图用'%20'替换用户输入的所有空格。我已经找到了一种这样的方法,但是一次只能处理一个字母,而我需要使用一个完整的字符串而不是一个字符数组。这是我尝试过的方法:
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
using std::string;
using std::cout;
using std::endl;
using std::replace;
replace(s_input.begin(), s_input.end(), ' ', '%20');
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input.c_str());
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
如果您拥有Visual Studio 2010或更高版本,则应该可以使用正则表达式来搜索/替换:
std::regex space("[[:space:]]");
s_input = std::regex_replace(s_input, space, "%20");
Run Code Online (Sandbox Code Playgroud)
编辑:如何使用的六参数版本std::regex_replace:
std::regex space("[[:space:]]");
std::string s_output;
std::regex_replace(s_output.begin(), s_input.begin(), s_input.end(), space, "%20");
Run Code Online (Sandbox Code Playgroud)
字符串s_output现在包含更改后的字符串。
您可能需要将替换字符串更改为std::string("%20")。
如您所见,我只有五个参数,这是因为第六个参数应具有默认值。
| 归档时间: |
|
| 查看次数: |
1583 次 |
| 最近记录: |