c++ string与String区别
发布时间:2021-07-11 栏目:C++ 评论:0 Comments
C++中只有string,String可以用作自定义标识符作为变量使用。
string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行string操作,也可以通过文件等手段进行string操作。同时,C++的算法库对string类也有着很好的支持,并且string类还和c语言的字符串之间有着良好的接口。
2. 常见用法
2.1 string转换为char*
2.1.1 data()方法与c_str()方法
data()方法与c_str()方法相似,都返回 const char* 类型。两者区别和联系如下:
在C++98版本中,c_str()返回 const char* 类型,返回的字符串会以空字符(null character)结尾;
在C++98版本中,data()返回 const char* 类型,返回的字符串不以空字符(null character)结尾;
在C++11版本中,c_str()与data()用法相同(Both string::data and string::c_str are synonyms and return the same value.)
2.2 计算string长度、string字符串比较
2.3 string对象判空
可使用 empty() 方法对string类型的对象进行判空
2.4 char*、char[]转换为string
2.5 string类的find方法
使用string类的find方法,在字符串中检索自字符串是否存在。
2.6 string类的insert方法
使用string类的insert方法,向字符串中插入字符(串)。
2.7 int类型转为string类的方法
留下评论
You must be logged in to post a comment.
近期评论
- Pika发表在《莫里斯蠕虫(Morris Worm)》
- Pika发表在《多组学科研分析》
- crisy发表在《最近关于专利的一点感想》
- walter发表在《机器学习基础知识回顾-马尔科夫过程(Markov Process)》
文章归档
- 2024年3月
- 2024年2月
- 2023年12月
- 2023年11月
- 2023年10月
- 2023年9月
- 2023年8月
- 2023年7月
- 2023年6月
- 2023年5月
- 2023年4月
- 2023年3月
- 2023年2月
- 2023年1月
- 2022年12月
- 2022年11月
- 2022年9月
- 2022年8月
- 2022年7月
- 2022年6月
- 2022年5月
- 2022年3月
- 2022年2月
- 2022年1月
- 2021年12月
- 2021年11月
- 2021年10月
- 2021年9月
- 2021年8月
- 2021年7月
- 2021年6月
- 2021年5月
- 2021年4月
- 2021年2月
- 2021年1月
- 2020年12月
- 2020年11月
- 2020年10月
- 2020年8月
- 2020年7月
- 2020年6月
- 2020年5月
- 2020年4月
- 2020年3月
- 2020年2月
- 2019年7月
- 2019年5月
- 2019年3月
- 2019年1月
- 2018年6月
- 2018年5月
- 2018年4月
- 2018年3月
- 2018年2月
- 2017年11月
- 2017年7月
- 2017年6月
- 2017年5月
- 2017年3月
- 2016年12月
- 2016年11月
- 2016年10月
- 2016年9月
- 2016年8月
- 2016年7月
- 2016年6月
- 2016年5月
- 2016年4月
- 2016年3月
- 2016年2月
- 2016年1月
- 2015年12月
- 2015年11月