George Irimiciuc
George Irimiciuc

Reputation: 4633

Add forward slash to string

I want to make a string "test/" but I can't add the slash after the initial string. Any idea why and how?

string imgpath="test";
strcat(imgpath,"/");

This is what I've tried so far. I get

Error   1   error C2664: 'strcat' : cannot convert parameter 1 from 'std::string' to 'char *'

And another

imgpath="test"+"/";

Error   1   error C2110: '+' : cannot add two pointers

Upvotes: 3

Views: 12837

Answers (2)

Bill Lynch
Bill Lynch

Reputation: 81936

strcat is used to append to a c-string. You should just use string::append or string::operator+=:

imgpath.append("/");
imgpath += "/";

For your second question: "asd" is a char *, not a std::string. So it doesn't have a useful + operator. This code should look like:

string x = string("asd") + "xyz";

Upvotes: 4

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Use the std::string::operator+=() instead of strcat().

string imgpath="test";
imgpath += "/";

As for your second example

imgpath=std::string("test") +"/";

Upvotes: 6

Related Questions