[C++] split string on "name=test" and get "test" into a string without "name="
Go to solution
Solved by Unimportant,
41 minutes ago, WillLTT said:im doing standard vs19 c++.
im trying to split
//"test" is changed by what user input is. std::string name = "name=test";into a string and get "test" into a string. however i dont want "name=" in there.
and then cout the result
std::cout << name;so basically remove "name=" from the string.
One possibility:
#include <iostream> #include <string> int main() { const std::string name = "name=test"; //check if "name=" is present in string and if it starts at position 0. const auto startIndex = name.find("name="); if (startIndex == std::string::npos || startIndex) { std::cout << "Did not find \"name=\" or it was not located at beginning of string.\n"; return 1; } //split string. const auto subName = name.substr(std::string("name=").length()); //optionally test result length. Will be empty if there's nothing after "name=" if (!subName.length()) { std::cout << "Result empty."; return 2; } std::cout << subName << '\n'; return 0; }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now