C++常用的字符指针/字符数组/字符串/数字之间的转换

本文详细介绍了C++中字符串类型如char*、const char*、char[]与string之间的转换,包括string到这些类型的转换。同时,讲解了const char*与char*的转换,数字与string的互转方法,并展示了string的一些常用操作,如插入、拼接、查找和替换。最后,提到了字符处理函数sprintf()的使用,及其在格式化输出上的威力。
摘要由CSDN通过智能技术生成

目录

1.char* ,const char*,char[]与string 之间的转换

2.string与char* ,const char*,char[] 之间的转换

3.const char* 与 char* 之间的转换

4.数字与string之间的转换

5.string的操作 (Java语言的操作与之类似)

6.字符处理函数 sprintf()


1.char* ,const char*,char[]与string 之间的转换

char *m=(char*)"hello world!";//char *m="hello world!";公司编译器编译不过,不能直接赋值,要强制转换
	const char *p="nice day";
	char n[]="hello 2 world!!";
	string k=m;
	cout <<"\n k:" << k << "k.length:"  << k.length() << endl;
	k=p;
	cout <<"\n k:" << k << "k.length:"  << k.length() << endl;
	k=n;
	cout <<"\n k:" << k << "k.length:"  << k.length() << endl;	
	string kkk(m);//使用 string的构造函数初始化
	cout <<"\n kkk:" << kkk << "kkk.length:"  << kkk.length() << endl;

2.string与char* ,const char*,char[] 之间的转换

string kk="dten";
	printf("kk:%s",kk.c_str());//printf("kk:%s",kk);编译不过,参数需要 char* 类型
	//char* p_0=kk.data();//公司环境编译不过
	//char* p_1=kk.c_str();//公司环境编译不过, c_str()返回的只是临时指针不能对其操作,但是能作为函数的参数

	char* p_1=new char[kk.length()+1];
	strcpy(p_1,kk.c_str());
	cout <<"\n p_1:" << *p_1 << endl;//这里仅仅输出的是一个字符	
	printf("p_1:%s",p_1);

	const char* p_2=kk.c_str();
	cout <<"\n p_2:" << *p_2 << endl;
	printf("p_2:%s",p_2);

	char n_1[kk.length()+1];
	strcpy(n_1,kk.c_str());
	cout <<"\n n_1:" << n_1 << endl;

3.const char* 与 char* 之间的转换

	const char* cpc="abcdefg";
	char* pc=new char[100];
	strcpy(pc,cpc);
	printf("pc:%s",pc);

4.数字与string之间的转换

注意导入头文件 #include <bits/stdc++.h>

int num=0;
	string str="123";
	num=stoi(str);//string 转换为 int,还有stol()字符串转换为长整型,stoll()字符串转换为长长整型
	cout <<"\n num:"<<num << endl;
	
	string str_2="5.678";
	char c_array[100]="3.1415";
	double d_num=atof(c_array);//string 转换为 double,注意这里本质上是利用了C语言的转换函数
	cout <<"\n d_num:"<<d_num << endl;
	double d_2=atof(str_2.c_str());
	cout <<"\n d_2:"<<d_2 << endl;
	
	int x=589;
	string msg=to_string(x);//int转换为string,另外该函数还支持长整型,长长整型的转换
	cout << "\n msg:" << msg << endl;

5.string的操作 (Java语言的操作与之类似)

string s1,s2,s3;
	s1=s2=s3="123456789";
	s3="aaaa";
	s1.insert(5,s3);
	cout << "\ns1:" <<s1<<"\n s2:" << s2<< "\n s3:"<< s3 << endl;
	string sss;
	sss=s1+s3;//直接使用+号拼接
	cout <<"\nsss:" << sss << endl;

	s2="helloworld12317890weqwer";
    int index=s2.find("world");
    cout <<"\n index:" << index << endl;
    index=s2.find_first_of("o");//第一次出现的索引
    cout <<"\n first index:" << index << endl;
    index=s2.find_last_of("o");//最后一次出现的索引
    cout <<"\n last index:" << index << endl;
    s2.replace(5,3,"mmm");//用mmm从索引5开始替换s2的3个字符
    cout <<"\n s2:" << s2 << endl;

    string result=s2.substr(10);//从索引10处开始截取后面的字符
    cout <<"\n result:" << result << endl;
    result=s2.substr(10,5);
    cout <<"\n result:" << result << endl;

6.强大的字符处理函数 sprintf()

char array_2[100];
    sprintf(array_2,"qweqe%d456789",123);
    printf("\narray_2:%s\n",array_2);

字符串的格式化,对齐,字符保留位数的处理等,非常强大,详细参考:(7条消息) 整理:C++中sprintf()函数的使用详解_bat67的博客-CSDN博客_c++ sprintf

文章参考:

C++字符串相互转换 - wenglabs - 博客园 (cnblogs.com)

c++中char与string之间的相互转换问题 - 百度文库 (baidu.com)

​​​​​​(7条消息) C++数字转string,string转数字函数总结_HeisenbergWDG的博客-CSDN博客_c++ 数字转string

(7条消息) c++中数字与string转换_w55100的博客-CSDN博客_c++字符串转数字

​​​​​(7条消息) C++中将string类型转换为int, float, double类型 主要通过以下几种方式:_candadition的博客-CSDN博客_c++ string转float​​​​​​

(7条消息) [C++] string转为int, float, double_gz7seven的博客-CSDN博客_c++ string转float

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值