string 字符串连接
来源:互联网 发布:碎屏幕手机壁纸软件 编辑:程序博客网 时间:2024/05/22 14:14
有了 string 类,我们可以使用”+“或”+=“运算符来直接拼接字符串,非常方便,再也不需要使用C语言中的strcat()、strcpy()、malloc()等函数来拼接字符串了,再也不用担心空间不够会溢出了。
用”+“来拼接字符串时,运算符的两边可以都是string 字符串,也可以是一个string 字符串和一个C风格的字符串,还可以是一个string 字符串和一个char 字符。
请看下面的例子:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1, s2, s3;
s1 = "first";
s2 = "second";
s3 = s1 + s2;
cout<< s3 <<endl;
s2 += s1;
cout<< s2 <<endl;
s1 += "third";
cout<< s1 <<endl;
s1 += 'a';
cout<< s1 <<endl;
return 0;
}
运行结果:
firstsecond
secondfirst
firstthird
firstthirda
还有一种就是使用append函数
例如string a = “123”;
string b = “456”;
a.append(b);//132456
阅读全文
0 0
- 字符串连接(string语句 )
- 连接字符串 Connection String
- string 字符串的连接
- String.concat连接字符串
- string 字符串连接
- string 字符串连接
- 使用STL string连接字符串
- String与StringBuilder连接字符串
- string实现两个字符串的连接
- 【c1】字符串连接 string (code)
- String和StringBuffer连接字符串比较
- 将两个字符串连接起来 用string
- java字符串连接String、StringBuffer和StringBuilder
- Java字符串连接效率 String、StringBuilder、StringBuffer
- C#控制台基础 string字符串的连接
- C#控制台基础 string字符串的连接
- 为什么String连接字符串很慢
- 不调用C库函数string.h实现字符串的连接
- 基于vue的移动web app页面缓存解决方案
- 爬虫---requests库
- Spring支持国际化
- Activiti工作流引擎数据库表结构
- vue-router使用总结
- string 字符串连接
- 最短路uva11374
- GC垃圾回收
- SpringMVC上传图片并预览
- caffe中 对caffemodel进行结构修改并保存
- Solr集群
- c++继承经典例子
- POJ 1151 Atlantis(扫描线求面积)
- YARN 状态机库