I've one variable in which i get string value from user. Now i want to use that string value in URL. like my string value is "Indore". so it should pass like URL="awe/Indore/ddsd". Please tell me how to solve?
1 Answer
I think first we need to know what "String" type are you working with and maybe provide examples so that we can better help you. From what I see from your comment, you can split the string first then merge them together.
If it is a C string char* then you can use strtok to split (see another topic). Then merge with strcat as Ehsan Khodarahmi mentioned, (see another topic)
If it is a std::string string, then you can try my implementation in c++ for splitting std::string string.
std::vector<std::string> splitString(std::string inputString, std::string delimiter){
std::string temp = inputString;
std::vector<std::string> output;
size_t pos = 0;
std::string token;
while ((pos = temp.find(delimiter)) != std::string::npos) {
token = temp.substr(0, pos);
output.push_back(token);
temp.erase(0, pos + delimiter.length());
}
output.push_back(temp);
return output;
}
In your case, you can do
std::vector<std::string> splitted = spitString("Capital/Madhya", "/");
Then splitted = {"Capital", "Madhya"}, and you can just do
std::string url = splitted[0] + "/Indore/" + splitted[1];
As people said in the comment, your question is vague, so my answer may not directly help you.
Update
If you are using c_str() then you are probably using std::string. c_str() is used for converting std::string to c string char* (see reference). So in this case, you can use my code provided to assist you. From what you told me, user input was passed to var1.
std::string var1 = "Russia"
And you already have var2
std::string var2 = "Capital/Japan"
Then simply split the var2 by / and merge what you want.
std::vector<std::string> splitted = splitString(var2, "/");
std::string result = splitted[0] + "/" + var1 + "/" + splitted[1];
Then your result should be "Capital/Russia/Japan".
Update2
First, thanks for include code this time. You should do that all the time if possible so people know what to work with. Note: To format your code, enclose your code with ```
Now to your question. You provided this
#include <iostream>
#include <vector>
using namespace std;
int main() {
std::string var1 = "Russia";
std::string var2 = "Capital/Japan";
std::vector<std::string> splitted = splitString(var2, "/");
std::string result = splitted[0] + "/" + var1 + "/" + splitted[1];
cout<<result; return 0;
}
And your compiler doesn't like it and says
error: 'splitString' was not declared in this scope
This error message basically says that splitString function does not exist. This is because you did not include my code in your code. At some point you will get more comfortable reading these error messages.
So the fix is
#include <iostream>
#include <vector>
using namespace std;
std::vector<std::string> splitString(std::string inputString, std::string delimiter){
std::string temp = inputString;
std::vector<std::string> output;
size_t pos = 0;
std::string token;
while ((pos = temp.find(delimiter)) != std::string::npos) {
token = temp.substr(0, pos);
output.push_back(token);
temp.erase(0, pos + delimiter.length());
}
output.push_back(temp);
return output;
}
int main() {
std::string var1 = "Russia";
std::string var2 = "Capital/Japan";
std::vector<std::string> splitted = splitString(var2, "/");
std::string result = splitted[0] + "/" + var1 + "/" + splitted[1];
cout<<result; return 0;
}
strcatmethodstd::regexcan provide you with some useful operations to insert to the URL that you finally need.