简体   繁体   English

将正斜杠添加到字符串

[英]Add forward slash to string

I want to make a string "test/" but I can't add the slash after the initial string. 我想创建一个字符串“test /”但我不能在初始字符串后添加斜杠。 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

Use the std::string::operator+=() instead of strcat() . 使用std::string::operator+=()代替strcat()

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

As for your second example 至于你的第二个例子

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

strcat is used to append to a c-string. strcat用于附加到c-string。 You should just use string::append or string::operator+= : 你应该只使用string::appendstring::operator+=

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

For your second question: "asd" is a char * , not a std::string . 对于你的第二个问题: "asd"是一个char * ,而不是一个std::string So it doesn't have a useful + operator. 所以它没有一个有用的+运算符。 This code should look like: 此代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM