C++中的Compare()函数用法
来源:互联网 发布:阿里云测试培训 编辑:程序博客网 时间:2024/06/05 05:42
#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void){
string str1="hi,test,hello";
string str2="hi,test";
//字符串比较
if(str1.compare(str2)>0)
printf("str1>str2\n");
else if(str1.compare(str2)<0)
printf("str1<str2\n");
else
printf("str1==str2\n");
//str1的子串(从索引3开始,包含4个字符)与str2进行比较
if(str1.compare(3,4,str2)==0)
printf("str1的指定子串等于str2\n");
else
printf("str1的指定子串不等于str2\n");
//str1指定子串与str2的指定子串进行比较
if(str1.compare(3,4,str2,3,4)==0)
printf("str1的指定子串等于str2的指定子串\n");
else
printf("str1的指定子串不等于str2的指定子串\n");
//str1指定子串与字符串的前n个字符进行比较
if(str1.compare(0,2,"hi,hello",2)==0)
printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");
else
printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");
return 0;
}
#include <string>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void){
string str1="hi,test,hello";
string str2="hi,test";
//字符串比较
if(str1.compare(str2)>0)
printf("str1>str2\n");
else if(str1.compare(str2)<0)
printf("str1<str2\n");
else
printf("str1==str2\n");
//str1的子串(从索引3开始,包含4个字符)与str2进行比较
if(str1.compare(3,4,str2)==0)
printf("str1的指定子串等于str2\n");
else
printf("str1的指定子串不等于str2\n");
//str1指定子串与str2的指定子串进行比较
if(str1.compare(3,4,str2,3,4)==0)
printf("str1的指定子串等于str2的指定子串\n");
else
printf("str1的指定子串不等于str2的指定子串\n");
//str1指定子串与字符串的前n个字符进行比较
if(str1.compare(0,2,"hi,hello",2)==0)
printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");
else
printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");
return 0;
}
阅读全文
0 0
- C++中的Compare()函数用法
- substr()、compare()、string中的find()函数、
- C++ 优先队列用法自定义Compare函数
- C/C++中的字符串比较函数strcmp/memcmp/CString.Compare/CString:CompareNoCase
- C函数中的memcpy的用法(本文为转载!!!)
- C++Builder 中的 Format 函数及其用法。
- C语言中的Sleep函数用法
- c中的memset函数的用法?
- Linux C中的basename函数用法示例
- C语言中的strstr函数的用法
- c语言中的time函数用法
- c语言中的内联函数用法解析
- c++compare
- c语言qsort()函数中compare()函数参数写法
- Objective—C中的排序及Compare陷阱
- C#_字符串中的Compare和Equal方法
- string .Compare的用法
- String.Compare的用法
- pandas.DataFrame.append
- 员工部门mysql面试题
- 单片机与数字电路抗干扰措施
- java1.8 lambda表达式 函数式编程 闭包
- Canvas基础知识
- C++中的Compare()函数用法
- Android工程方法数超过65535,怎么破
- 赫夫曼编码
- 重新搭建eclipse、android、tomcat平台
- Android 仿火萤视频桌面 神奇的LiveWallPaper
- hadoop部署参考
- Git命令
- 单点登录原理与简单实现
- iOS IPV6审核被拒