C++ json、string、char[]转换和赋值

这里记录下以上几个类型之间的转换和赋值,方便项目使用的时候能快速解决。

1.char[]赋值给string

char s_sex[] = "boy";
char s_name[32] = {0};
std::string temp_string;
memcpy(s_name,"zhangsan",strlen("zhangsan"));

//方式一:用 “=” 赋值
temp_string = s_name;

//方式二:用 assign() 函数
temp_string.assign(s_sex);

注意:使用char字符串数组给string赋值时,确保有’\0’结束符。

示例:

#include <string.h>
#include <iostream>
using namespace std;

int main()
{
	char s_info[] = "boy_2019_01_31";
	char s_name[32] = {0};
	memcpy(s_name,"zhangsan",strlen("zhangsan"));
	
	std::string temp_string;
	
	// “=” 赋值
	temp_string = s_name;
	cout << "temp_string: " << temp_string << endl;
	
	// assign() 函数
	temp_string.assign(s_info);
	cout << "temp_string: " << temp_string << endl;
	temp_string.assign(s_info,4);
	cout << "temp_string: " << temp_string << endl;
	temp_string.assign(s_info,1,3);
	cout << "temp_string: " << temp_string << endl;
	
	return 0;
}

2.char[]赋值json对象

char strtemp[128];
Json::Value config;

memset(strtemp,0,sizeof(strtemp));
memcpy(strtemp,"zhaosi",strlen("zhaosi"));

//一般可直接赋值,本质还要看Json::Value对象是怎么封装和实现的
config["name"] = strtemp;	//或 (char *)strtemp

//也可以临时转给std::string再赋值给json对象

注意:使用char字符串数组给Json::Value赋值时,确保有’\0’结束符。

3.std::string赋值给char[]

char s_str[64];
std::string str = "hello";

//方式一 std::string date()
memset(s_str,0,sizeof(s_str));
memcpy(s_str,str.data(),str.length());

//方式二 std::string c_str()
memcpy(s_str,str.c_str(),str.length());

//方式三 std::string copy()
std::size_t length = str.copy(s_str,str.length(),0);
s_str[length]='\0';

4.std::string赋值给json

std::string str;
Json::Value config;

str = "hello world!";
config["info"] = str;

直接赋值即可;

5.json赋值给char[]

char stringtemp[128];
Json::Value config;
config["custom"]["date"] = "hello work";

if(config.isMember("custom") && config["custom"].isObject())
{
	if(config["custom"].isMember("data")&& config["custom"]["data"].isString())
	{
		memset(stringtemp, 0, sizeof(stringtemp));
		memcpy(stringtemp,config["custom"]["data"].asCString(),config["custom"]["data"].asString().length());
		//或
		memcpy(stringtemp,config["custom"]["data"].asString().c_str(),config["custom"]["data"].asString().length());
	}
}

注意:使用Json::Value赋值给char[]字符串数组时,要判断是否存在、要注意封装接口类型、要确保char[]空间足够。

6.json赋值给string

std::string stringtemp;
Json::Value config;
config["custom"]["date"] = "hello work";

if(config.isMember("custom") && config["custom"].isObject())
{
	if(config["custom"].isMember("data")&& config["custom"]["data"].isString())
	{
		stringtemp = config["custom"]["data"].asString();
	}
}

直接赋值即可。

其他

1.std::string assign()函数

函数原型

string &operator=(const string &s);              把字符串s赋给当前字符串
string &assign(const char *s);                 用c类型字符串s赋值
string &assign(const char *s,int n);              用c字符串s开始的n个字符赋值
string &assign(const string &s);                把字符串s赋给当前字符串
string &assign(int n,char c);                  用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);       把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);  把first和last迭代器之间的部分赋给字符串

示例:

#include <iostream>
using namespace std;

int main()
{
    string str1("hello");
    string str2;
    string str3;
    string str4;
    string str5;

    str2.assign(str1);
    str3.assign("World", 4);
    str4.assign(str1, 2, 3);
    str5.assign(10, 'c');
    //====================================

    cout<<str1<<endl;
    cout<<str2<<endl;
    cout<<str3<<endl;
    cout<<str4<<endl;
    cout<<str5<<endl;

    system("pause");

    return 0;

}

2.std::string copy()函数

函数原型

size_t copy (char* s, size_t len, size_t pos = 0) const;

示例:

// string::copy 
#include <iostream>
#include <string>

int main ()
{
  char buffer[20];
  std::string str ("Test string...");
  std::size_t length = str.copy(buffer,str.length(),0);
  buffer[length]='\0';
  std::cout << "buffer contains: " << buffer << '\n';
  return 0;
}

3.std::string clear()函数

std::string str;
Json::Value config;

str = "hello world!";
config["info"] = str;

str.clear();		//从 string 移除所有字符,
str = "big world";	//感觉没有必要执行上面那一步,重新赋值不就好了,但是项目中好多clear
config["tag"] = str;

4.stringstream clear与str("")的问题

许多博客都有提到stringstream的清空,不应该调用clear,而是要调用str(""),传入一个空字符串来让其清空内容。

https://www.cnblogs.com/elenno/p/stringstream_clear.html
https://blog.csdn.net/jcq521045349/article/details/49888751

参考资料:
http://www.cnblogs.com/sylar5/p/6638189.html
http://www.cplusplus.com/reference/string/string/copy/

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值