将单个字符转换为字符串? [英] Convert a single character to a string?

查看:142
本文介绍了将单个字符转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单问题(在C ++中):



如何将字符转换为字符串。
例如,我有一个字符串str =abc;



我想提取第一个字母,但我想它是一个字符串$

  

string firstLetter = str [0] +;

  string firstLetter =& str [0]; 

无效。想要吗?

解决方案

如果你使用的是STL,
$ b

  string firstLetter(1,str [0]); 


Simple question (in C++):

How do I convert a character into a string. So for example I have a string str = "abc";

And I want to extract the first letter, but I want it to be a string as opposed to a character.

I tried

string firstLetter = str[0] + "";

and

string firstLetter = & str[0]; 

Neither works. Ideas?

解决方案

Off the top of my head, if you're using STL then do this:

string firstLetter(1,str[0]);

这篇关于将单个字符转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆