Hey, folks! In this article, we will be focusing on the working of C++ string compare() function along with its variants.

嘿伙计! 在本文中,我们将重点介绍C ++字符串compare()函数及其变体的工作



C ++中的String compare()入门 (Getting started with String compare() in C++)

C++ String library contains a huge set of functions to work with the string data and manipulate the characters.

C++ String library包含用于处理字符串数据和处理字符的大量函数。

The string.compare() in C++ compares the string values in a lexicographical manner i.e. it compares the ASCII values of the characters of the comparable strings.

string.compare() in C++string.compare() in C++字典方式比较字符串值,即,它比较可比较字符串的字符的ASCII值

Syntax:

句法:


string1.compare(string2)

The compare() function returns 0, if the strings are exactly the same.

如果字符串完全相同,则compare()函数将返回0。

And, if the strings are not equal, the function returns either of the following values:

并且,如果字符串不相等,该函数将返回以下值之一:

  • Value < 0: The function returns a value less than 0 if the ASCII value of the first non-matching character of string1 is less than ASCII value of the first character of string2.Value < 0 :如果string1的第一个不匹配字符的ASCII值小于string2的第一个字符的ASCII值,则该函数返回小于0的值。
  • Value > 0: The function returns a value greater than 0 if the ASCII value of the first non-matching character of string1 is greater than ASCII value of the first character of string2.Value > 0 :如果string1的第一个不匹配字符的ASCII值大于string2的第一个字符的ASCII值,则该函数返回大于0的值。

Example:

例:


#include<iostream>
using namespace std; int main()
{ string str1="Python"; string str2="Python with Journaldev"; if((str1.compare(str2))==0)cout<<"Strings are equal!!";elsecout<<"Strings are not equal!!";return 0;
} 

Output:

输出:


Strings are not equal!!


C ++中的字符串compare()的变体 (Variants of String compare() in C++)

C++ String compare() function can be used in different forms to compare the strings accordingly.

C ++字符串compare()函数可以以不同的形式用于相应地比较字符串。

Let us understand the different variants of the string.compare() function in the below section.

在下面的部分中,让我们了解string.compare() function的不同变体。



变体1:使用索引将字符串的一部分与其他字符串进行比较 (Variant 1: Comparing a portion of a string with the other using indexes)

In this variation, we can compare a portion of string1 with string2 by using the below syntax:

在此变体中,我们可以使用以下语法将string1的一部分与string2进行比较:

Syntax:

句法:


string1.compare(start-index, end-length, string2)
  • start-index: The starting index of the string1 to be compared with string2.start-index :要与string2比较的string1的起始索引。
  • end-length: The length of the string1 upto which the portion of string1 needs to be compared with string2.end-length :字符串1的长度,字符串1的一部分需要与之比较,字符串2的长度。

Example:

例:


#include<iostream>
using namespace std; int main()
{ string str1="Python"; string str2="Python with Journaldev"; if((str2.compare(0, 6, str1))==0)cout<<"Python with Journaldev contains Python";elsecout<<"Python with Journaldev does not contain Python";return 0;
} 

In the above example, the portion of characters of str2 from index 0 to length 6 i.e. ‘Python’ is compared with str1.

在上面的示例中,将str2的字符从索引0到长度6的部分(即“ Python”)与str1进行了比较。

Output:

输出:


Python with Journaldev contains Python


方案2:将字符串与C字符串的字符进行比较 (Variant 2: Comparing the string with the characters of the C-string)

The string.compare() function can be used to compare the string with the characters of the C-string format using the below syntax:

string.compare() function可用于使用以下语法将字符串与C字符串格式的字符进行比较:

Syntax:

句法:


string.compare(characters)

Example:

例:


#include<iostream>
using namespace std; int main()
{ string str1="Python"; string str2="Python with Journaldev"; if((str1.compare("Python")) == 0)cout<<"Strings are equal!\n";elsecout<<"Strings are not equal!";if((str2.compare("Journaldev")) > 0)cout<<"Python with Journaldev is greater than Journaldev";elsecout<<"Python with Journaldev is less than Journaldev";return 0;
} 

Output:

输出:


Strings are equal!
Python with Journaldev is greater than Journaldev


方案3:使用string.compare()函数比较字符串的一部分 (Variant 3: Comparing portion of strings using string.compare() function)

The string.compare() function can help us compare portions of the strings using the indexes.

string.compare()函数可以帮助我们使用索引比较字符串的各个部分。

Syntax:

句法:


string1.compare(start-index, end-length, string2, start-index, end-length)

Example:

例:


#include<iostream>
using namespace std;int main()
{ string str1="Python"; string str2="Python with Journaldev"; if((str1.compare(0, 6, str2, 0, 6)==0))cout<<"Strings are equal!\n";elsecout<<"Strings are not equal!";return 0;
} 

Output:

输出:


Strings are equal!


变体4:比较字符串的一部分和C字符串 (Variant 4: Comparing portion of string with C-string)

We can compare some part of the string with the C-string by specifying the index using the below syntax:

通过使用以下语法指定索引,我们可以将字符串的某些部分与C字符串进行比较:

Syntax:

句法:


string.compare(start-index, end-length,'C-string')

Example:

例:


#include<iostream>
using namespace std; int main()
{ string str1="Python"; //string str2="Python with Journaldev"; if((str1.compare(0, 6, "Python") == 0))cout<<"Strings are equal!\n";elsecout<<"Strings are not equal!";return 0;
} 

Output:

输出:


Strings are equal!


变体5:使用compare()函数将字符串的部分(字符)与C字符串的一部分进行比较 (Variant 5: Comparing portion(characters) of string with a portion of C-string using compare() function)

Syntax:

句法:


string.compare(start-index, end-length,'C-string',end-length)

Example:

例:


#include<iostream>
using namespace std; int main()
{ string str1="Python"; //string str2="Python with Journaldev"; if((str1.compare(0, 6, "Python",6) == 0))cout<<"Strings are equal!\n";elsecout<<"Strings are not equal!";return 0;
} 

Output:

输出:


Strings are equal!


结论 (Conclusion)

Thus, we have come to the end of the explanation. In this article, we have understood the basic working of string compare() function along with the variations which can be made in the function.

这样,我们就结束了解释。 在本文中,我们了解了字符串compare()函数的基本工作原理以及该函数可以进行的各种变化。



参考资料 (References)

  • C++ Tutorial — JournalDevC ++教程— JournalDev

翻译自: https://www.journaldev.com/39952/string-compare-in-c-plus-plus

如何在C ++中使用String compare()?相关推荐

  1. C++中的string::compare的使用

    在C++中使用std::string编写字符串相关操作时,我经常使用find方法,其实在有些场景下需要判断字符串是否相同,因而需要使用compare方法.下面是我的测试样例: //descriptio ...

  2. c 函数 字符串 find_如何在C ++中使用字符串find()

    c 函数 字符串 find In this article, we'll take a look at how we can use String find() in C++. 在本文中,我们将研究如 ...

  3. php把int转string,如何在php中实现int转string

    如何在php中实现int转string 发布时间:2020-07-20 09:22:45 来源:亿速云 阅读:83 作者:Leah 如何在php中实现int转string?针对这个问题,这篇文章详细介 ...

  4. 如何在Java中初始化List <String>对象?

    本文翻译自:How to initialize List object in Java? I can not initialize a List as in the following code: 我 ...

  5. 如何在C++中方便的将float、int等类型数据转换成string类型,并利用ROS中的std_msg/String发布出去

    在ROS系统中有时候我们需要利用标准的消息类型如std_msg/String等进行发布某些数据,这就需要将不同的数据类型进行相互转化,比如: float -->string      int-- ...

  6. 如何在Java中比较日期? [重复]

    本文翻译自:How to compare dates in Java? [duplicate] This question already has answers here : 这个问题已经在这里有了 ...

  7. 如何在JavaScript中比较数组?

    本文翻译自:How to compare arrays in JavaScript? I'd like to compare two arrays... ideally, efficiently. 我 ...

  8. 如何在PHP中获取客户端IP地址[重复]

    本文翻译自:How to get the client IP address in PHP [duplicate] This question already has an answer here: ...

  9. react 中渲染html_如何在React中识别和解决浪费的渲染

    react 中渲染html by Nayeem Reza 通过Nayeem Reza 如何在React中识别和解决浪费的渲染 (How to identify and resolve wasted r ...

最新文章

  1. 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码......
  2. 查看某个方法在哪里被调用_MATLAB局部函数公有化的方法: localfunctions
  3. 十步让你成为一名优秀的 Web开发人员
  4. 如何把Kubernetes config view里的base64编码过后的secret信息还原
  5. (转)Web Services使用多态(XmlInclude) ,支持自定义类型
  6. 开发个好的RTMP播放器到底难在哪里?RTMP播放器对标和考察指标
  7. Android逆向笔记-查看app的log打印(含调试方式打开)
  8. python中mean算函数吗_Python numpy.mean函数方法的使用
  9. 20190922 On Java8 第二十一章 数组
  10. 算法:回溯十一 Subsets数组的子数组集合4种解法
  11. 人社部《劳动合同》通用范本模板
  12. 显卡用什么软件作压力测试,推荐一个显卡的跑分跟压力测试的软件TimeSpy
  13. CTFHUB刷题 密码口令/默认口令
  14. 杨咩咩的编程求学之路之开篇
  15. 一个能和企鹅一样飞的创意
  16. excel单元格内容拆分_EXCEL批量拆分单元格,也可以这么快
  17. Tomcat运行成功但是报500错误
  18. Taro下拉刷新,上拉加载更多
  19. 【XSY4041】搬砖(线段树)
  20. 【项目一】医疗实战-传智健康

热门文章

  1. CentOS系统里如何正确取消或者延长屏幕保护自动锁屏功能(图文详解)
  2. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
  3. FreeMarker 基础语法教程
  4. 男性护肤不“美白” 控油:男女有别 - 生活至上,美容至尚!
  5. ASP.NET程序中常用代码汇总-1
  6. 傅里叶变换的终极解释上
  7. 单工、半双工、双工通信详解
  8. CMake 学习笔记 02 - 更复杂的项目
  9. 2018-2019-1 20189208《Linux内核原理与分析》第九周作业
  10. 2017.8.15 数据库