繁体   English   中英

C ++将字符串[4]转换为字符串

[英]C++ Converting string[4] to a string

我想将单词的最后一个字符更改为字符串。

谢谢!

编辑:添加乔恩的尝试的答案,因为它提供了一些洞察力,他的功能要求是:

string x = "apple"; char c = apple[4]; string q = "";
string z = q+c;
std::string x = "apple";
std::string z(x.substr(4, 1));

能行吗?

string x = "apple";
char c = apple[4];
string q = "";

string z = q+c;

如果您有一个char [],可以说char arr [] =“ Lalelu THis是一个带有单词的单词:P”;

然后,您可以尝试:

std::string str; str.assign(&arr[strlen(arr)-5], 4);

尝试这个..

std::string apple = "apple";
std::string fpp(apple.rbegin(), apple.rbegin() + 1);

尝试这样的事情:

string MyString = "Whee!"; // String to extract letter from.

char LastChar = MyString.at(MyString.length() - 1); // Retrieves the last letter of the string and stores it as a char.
string LastCharAsString = string(1, LastChar); // Typecasts the char to a string.

未经测试,因为我无权使用编译器ATM。

暂无
暂无

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

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