string 字符串连接
来源:互联网 发布:max809 数据手册 编辑:程序博客网 时间:2024/05/24 06:32
有了 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实现字符串的连接
- SpringBoot问题(Tomcat启动失败)
- c++primer5 3.17
- 共用体的应用(合并单元格)
- Ubuntu16.04 安装openjdk-7-jdk
- 解决Eclipse中的卡死现象
- string 字符串连接
- windows环境下 android 源码阅读
- 使用 JMeter 分布式性能测试
- js将时间戳转yyyy-mm-dd
- python3校验身份证号码
- Hibernate之缓存(一级缓存及二级缓存)
- Product has no PO record
- android入门注意事项
- 一个好玩的东西 php+html做的