Snowman288
Snowman288

Reputation: 107

How to get first two chars from a string and use the two chars in another string

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 string combined = 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.

Upvotes: 6

Views: 29893

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490048

The error you're currently getting is from this line:

CString fileToOpen = "\"\\\\CAR\\VOL1\\Docs\\PRE\\15\\" + combined.c_str() + "\\" + filenum.c_str() + ".prt" + "\"";

...because it tries to add pointers. The string literal evaluates to a pointer to the beginning of the storage for the literal. Likewise, combined.c_str() yields a pointer to the storage used for its content. Adding those pointers together doesn't work (and, of course, you have more of the same throughout the rest of the expression).

You usually want to do the manipulation on std::string objects, then when you've done all the manipulation, you can create the CString from the result. (Alternatively, you could use CStrings throughout, about equivalently assuming you're using MFC's CString). Either way, you want to avoid trying to do much manipulation on raw pointers.

Given the number of back-slashes you have here, you might consider using raw strings for at least the one leading literal. I'd also put it into a std::string before doing the manipulation:

  std::string prefix = R"-("\\CAR\VOL1\Docs\PRE\15\)-";

Then creating the concatenated string should look something like this:

CString fileToOpen = (prefix + combined + "\\" + filenum + ".prt\"").c_str();

Upvotes: 2

therainmaker
therainmaker

Reputation: 4343

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;

Upvotes: 18

Related Questions