简体   繁体   English

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

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

I want to change the last character of a word to be a string. 我想将单词的最后一个字符更改为字符串。

thanks! 谢谢!

EDIT: adding Jon's attempted answer as it provides some insight into what his functional requirement is: 编辑:添加乔恩的尝试的答案,因为它提供了一些洞察力,他的功能要求是:

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

Could this work? 能行吗?

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

string z = q+c;

If you got a char[] lets say char arr[] = "Lalelu THis is a for-worded word:P"; 如果您有一个char [],可以说char arr [] =“ Lalelu THis是一个带有单词的单词:P”;

Then you could try: 然后,您可以尝试:

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

try this.. 尝试这个..

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

Try something like this: 尝试这样的事情:

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.

Untested, as I don't have access to a compiler ATM. 未经测试,因为我无权使用编译器ATM。

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

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