C++字符串函数与C字符串函数比较
来源:互联网 发布:mesh网络 编辑:程序博客网 时间:2024/06/05 19:02
赋值拷贝:
#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和memmovechar 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; //012345678string 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,大于返回1cout<<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; //0bcd34567b.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; //012a = "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::nposcout<<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'的位置,等价于rfindcout<<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 参数为stringcout<<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和bcout<<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; //12345d="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;}
0 0
- c 字符串比较函数
- C++字符串函数与C字符串函数比较
- C语言字符串比较函数
- C语言字符串输入函数的比较与解析
- C语言函数与字符串
- C语言 字符串与字符串函数
- C语言字符串函数与C++string的字符串函数
- C的内存和字符串比较函数
- (C语言)字符串比较函数
- C -- 字符串和字符串函数
- c++(字符串处理函数)
- C语言字符串函数
- C语言字符串函数
- c字符串函数源码
- c 字符串转换函数
- C字符串处理函数
- C字符串函数
- C字符串处理函数
- abercrombie italia more than reflect the requirements of the national economy as a pilot area of
- axure 7.0 注册码
- christian louboutin pas cher the past three years
- 关于long long 的输入和输出
- air max femme and the addition of fleet operational command system
- C++字符串函数与C字符串函数比较
- 109_利用反射调用隐藏api获取应用程序的缓存大小
- rayban pas cher Shenzhen Branch
- 执行命令mm出错以及运行adb shell出现insufficient permissions for device错误的解决方法
- 经典问题—链表中的环问题
- Neutron特性介绍之一 - 虚拟网络组网基本需求
- POJ 3342 树形DP入门题
- javascript中数组Array的方法
- servlet&jsp 登陆,根据输入显示不同页面内容