I would like to get the first two chars from my string. Lets say my string dbdir = "Dir"
and my other string test = "20122"
. I want to get the first two chars from test and combine it with dbdir
string. So the result would be a string Dir20
then I want to use the combined string in another string for a file.
Here is my code
std::string dbdir = "Dir";
std::string test = "20122";
//strip first two chars from test//
std::string result_of_test_strip = ;
std::string combined = ""+ dbdir + result +"";
CString fileToOpen = "\"\\\\CAR\\VOL1\\Docs\\PRE\\15\\" + result_of_test_strip.c_str() + "\\" + filenum.c_str() + ".prt" + "\"";
Suggested answer @therainmaker
std::string dbdir = "Dir";
std::string test = "20122";
std::string result = test.substr(0, 2);
std::string combined = dbdir + result;
CString fileToOpen = "\"\\\\CAR\\VOL1\\Docs\\PRE\\15\\" + combined.c_str() + "\\" + filenum.c_str() + ".prt" + "\"";
I get this error in CString fileToOpen ---> error C2110: cannot add two pointers Error executing cl.exe.
You can use the substr
function to extract any relevant portion of a string.
In your case, to extract the first two characters, you can write
string first_two = test.substr(0, 2) // take a substring of test starting at position 0 with 2 characters
Another method for the first two characters might be
string first_two;
first_two.push_back(test[0]);
first_two.push_back(test[1]);
Also, in your string_combined
line, you don't need to add an empty string ""
at the beginning and end. The following line will work as well:
string combined = dbdir + result;