如何转换字符数组为一个字符串? [英] How to convert a char array to a string?

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

问题描述

转换一个C ++ 字符串为char数组是使用字符串 c_str 函数pretty straightorward和然后做的strcpy 。但是,如何做相反?

Converting a C++ string to a char array is pretty straightorward using the c_str function of string and then doing strcpy. However, how to do the opposite?

我有一个字符数组,如:改编的char [] =这是一个测试; 转换回:
字符串str =这是一个测试

I have a char array like: char arr[ ] = "This is a test"; to be converted back to: string str = "This is a test.

推荐答案

字符串类有一个构造函数一个NULL结尾的C字符串:

The string class has a constructor that takes a NULL-terminated C-string:

char arr[ ] = "This is a test";

string str(arr);


//  You can also assign directly to a string.
str = "This is another string";

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

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