赋值拷贝:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="hello world!";
string b;
//完整拷贝:
b = a; //C语言可以利用strcpy(des,src)将src字符数组的字符串复制到des字符数组中,但des空间要足够大
cout<<b<<endl; //输出hello world
//部分拷贝:
a="0123456";
b.assign(a,3,2); /*将a字符串中以下标3为起始位置,拷贝2个字符到b中。
b.assign(a,0,n)等价于strncpy(des,src,n) */
cout<<b<<endl;
//string类型拷贝到char数组:
char c[20]="";
a.copy(c,3,2); //将a中的字符串从下标2为起始位置拷贝3个字符到c数组中
cout<<c<<endl;
//补充:C中还有2种内存拷贝函数memcpy和memmove
char d[] = "abcdef";
char e[20] = "";
memcpy(e,d,strlen(d)); //从d中拷贝strlen(d)个字符到e中
//memmove(e,d,strlen(d)); //与memcpy类似
cout<<e<<endl;
}
字符串连接:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="0123";
string b="45678";
a=a+b; //类似C中的strcat(des,src)
cout<<a<<endl; //012345678
string c="0123";
string d="0123456";
c.append(d,4,3); //将d中以下标4为起点的3字符连接到c后面。c.append(d,0,n)类似于strncat(des,src,n)
cout<<c<<endl;// 0123456
}
字符串比较:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="01234563210";
string b="4563210";
cout<<(a==b)<<endl; //比较和a和b是否相等,不等的话返回bool值false(0)。相等的话返回true(1)
//与string对象比较
cout<<a.compare(b)<<endl; //strcmp(str1,str2)。str1小于str2返回-1,等于返回0,大于返回1
cout<<a.compare(4,7,b)<<endl; //将a中以下标4为起点的7个字符与b进行比较。
cout<<a.compare(6,5,b,2,5)<<endl; //将a中以下标6为起点的5个字符与b中以下标2为起点的5个字符进行比较
//与字符数组比较,compare支持对字符数组的比较,用法与string对象类似
char c[]="4560";
cout<<a.compare(c)<<endl;
char d[]="4563210";
cout<<a.compare(4,7,d)<<endl;
cout<<a.compare(6,5,d,2,5)<<endl;
}
字符串替换:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="01234567";
string b="abcd";
//string类操作
a.replace(1,2,b,1,3); //用b中以下标1为起点的三个字符取代a中以下标1为起点的2个字符。
cout<<a<<endl; //0bcd34567
b.replace(1,3,2,'0'); //用2个‘0’取代a中以下标1为起点的3个字符。
cout<<b<<endl; //a00
//字符数组操作
string c="01234567";
char d[]={"abcd"};
c.replace(1,2,d,1,3); //用d中以下标1为起点的三个字符取代c中以下标1为起点的2个字符。
cout<<c<<endl; //0bcd34567
}
字符串插入:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="012345";
string b="abcdef";
//参数为string对象
a.insert(3,b,2,4); //往a的下标为3的位置插入b中以下标2为起点的4个字符
cout<<a<<endl; //012cdef345
//参数为char型数组
string c="012345";
char d[]="abcdef";
c.insert(3,b,2,4);
cout<<c<<endl; //012cdef345
}
删除字符串:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="012345";
a.erase(); //清空字符串
cout<<a<<endl; //为空
a = "012345";
a.erase(3); //删除以下标3为起点的字符
cout<<a<<endl; //012
a = "012345";
a.erase(3,2); //删除以下标3为起点的2个字符
cout<<a<<endl; //0125
}
字符串查找:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="01233210";
/*#######################################################
# 正向查找 #
#######################################################*/
cout<<a.find('3')<<endl; //从开头查找第一次出现'3'的位置 ,没有找到返回string::npos
cout<<a.find('1',3)<<endl; //从下标为3为起点,查找第一次出现'1'的位置
//find_first_of与find用法相同
cout<<a.find_first_of('3')<<endl;
cout<<a.find_first_of('1',3)<<endl;
//find_first_not_of查找的是第一次不出现指定字符的位置
cout<<a.find_first_not_of('3')<<endl; //从开头查找第一次不是'3'的位置
cout<<a.find_first_not_of('1',3)<<endl; //以下标3为起点查找第一次不是'1'的位置
/*#######################################################
# 逆向查找 #
#######################################################*/
cout<<a.rfind('3')<<endl; //逆向查找第一次出现'3'的位置
cout<<a.find_last_of('3')<<endl; //查找最后出现'3'的位置,等价于rfind
cout<<a.find_last_of('1',3)<<endl; //设置下标3为起点,逆向查找
cout<<a.find_last_not_of('3')<<endl; //逆向查找第一次不是'3'的位置
cout<<a.find_last_not_of('1',3)<<endl; //设置起点逆向查找
//此外find和rfind也可以用于字符串的查找,char型和string型都可以
string b="12345678";
string c="3456";
cout<<b.find(c)<<endl; //2 参数为string
cout<<b.find("3456")<<endl;//2 参数为char *
char d[]="3456";
cout<<b.find(d)<<endl;//2 参数为char数组
cout<<b.find(c,1)<<endl; //设置下标1为起点
//但是find_first_of,find_last_of等都不是寻找子串,而是寻找参数任意字符第一次出现或最后出现的位置
string e="43";
cout<<b.find_first_of(e)<<endl;
/*2 实际上b中是不包含“43”这个子串的,但返回了结果2。b中下标为2的字符为3。
find_first_of("43"),就是在b中查找第一次出现4或者第一次出现3的位置。最先找到3所以返回3的位置
*/
}
字符串长度:
#include <iostream>
#include <string>
using namespace std;
void main(){
string a="12345";
cout<<a.size()<<endl; //求字符串长度,size和length类似
cout<<a.length()<<endl;
string b="abcde";
a.swap(b); //交换a和b
cout<<a<<endl;
cout<<b<<endl;
//string对象转char型数组
const char *c;
string d="12345";
c=d.data();//data()与c_str()作用都是讲string转换成char数组,返回值为const char *
cout<<c<<endl; //12345
d="123";
cout<<c<<endl; //123 可见改变d的内容会影响到c所指向内容
}
整形和字符串的转换:
#include <iostream>
using namespace std;
void main(){
char str[10];
int num = 123;
//函数原型 char * itoa(整数,字符数组,进制)
cout<<itoa(num,str,10)<<endl; //将num转换成10进制以字符串的形式存储在str中
cout<<itoa(num,str,2)<<endl; //将num转换成2进制以字符串的形式存储在str中
cout<<itoa(num,str,16)<<endl; //将num转换成16进制以字符串的形式存储在str中
//int atoi(字符数组)
char str2[]="1234";
cout<<atoi(str2)<<endl; //将字符串转换成整数
}
求子串:
#include <iostream>
#include <string>
using namespace std;
void main(){
string str1 = "12345#abcdef";
string::size_type i = str1.find('#',0);
cout<<i<<endl;
string str2 = str1.substr(0,i);
cout<<str2<<endl;
str1.erase(0, i + 1);
cout<<str1<<endl;
}