String Compare usage

Source: Internet
Author: User
String (1)
int compare (const string& str) const;
Substrings (2)
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& STR,
             size_t subpos, size_t sublen) const;
C-string (3)
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
Buffer (4)
int compare (size_t pos, size_t len, const char* S, size_t n) const;
Compare strings compares the value of the string object (or a substring) to the sequence of characters of Rguments.

The compared string is the value of the string object or-if the signature used has a POS and a Len parameters-the su Bstring that begins at the character in position Pos and spans Len characters.

This string is compared to a comparing string, and which is determined by the other arguments to the function. Parametersstr Another string object, used entirely (or partially) as the comparing string.
POS Position of the The the ' the ' character in the compared string.
If It is greater than the string length, it throws Out_of_range.
Note:the the character is denoted by a value of 0 (not 1).
Len Length of compared string (if the string is shorter, as many characters as possible).
A value of String::npos indicates all characters until the end of the string. Subpos, Sublen Same as POS and Len above, but for the comparing string.
s pointer to an array of characters.
If argument n is specified (4), the The "the" characters in the array are used as the comparing string.
Otherwise (3), a null-terminated sequence is expected:the length of the sequence with the characters The string is determined by the occurrence of a null character.
n number of characters to compare. size_t is a unsigned integral type (the same as member type String::size_type). return ValueReturns a signed integral indicating the relation between:
value relation between compared string and comparing string
0 They Compare Equal
<0 Either the value of the ' the ' of the ' the ' of the ' character ' does not ' match is lower in ' compared string, or all compared characters MA Tch but the compared string is shorter.
>0 Either the value of the ' the ' of the ' the ' of the ' character ' does not the ' match is ' greater in ' compared string, or all compared Match but the compared string is longer.
Example
1
2
3
4
5
6
7 8 9
23 (
est
)
Comparing apples with apples
#include <iostream>
#include <string>

int main ()
{
  std::string str1 ("Green apple");
  std::string str2 ("Red apple");

  if (Str1.compare (STR2)!= 0)
    std::cout << str1 << "is not" << str2 << ' \ n ';

  if (Str1.compare (6,5, "apple") = = 0)
    std::cout << "Still," << str1 << "is a apple\n";

  if (Str2.compare (Str2.size () -5,5, "apple") = = 0)
    std::cout << "and" << str2 << "is also a apple\n ";

  if (Str1.compare (6,5,str2,4,5) = = 0)
    std::cout << "Therefore, both are apples\n";

  return 0;
}
Edit & Run

Output:
Green Apple is not red Apple
Still, green Apple are an apple and
Red Apple are also an Apple
therefore, both are Apples
Example:
#include <iostream>
#include <string>
using namespace Std;
	int main (void) {string str1= "Hi,test,hello";
	String str2= "Hi,test";
	string comparison if (Str1.compare (str2) >0) printf ("str1>str2\n");
	else if (Str1.compare (str2) <0) printf ("str1<str2\n");
	
	else printf ("str1==str2\n");
	The substring of the STR1 (starting with index 3, containing 4 characters) is compared to str2 if (Str1.compare (3,4,STR2) ==0) printf ("STR1 's specified substring equals str2\n");
	
	else printf ("str1 specified substring is not equal to str2\n");
	STR1 specifies that the substring is compared to the specified substring of the str2 if (Str1.compare (3,4,str2,3,4) ==0) printf ("STR1 's specified substring equals the specified substring of str2 \ n");
	
	else printf ("str1 specified substring is not equal to a specified substring of str2 \ n");
	STR1 specifies that the substring is compared to the first n characters of the string if (Str1.compare (0,2, "Hi,hello", 2) ==0) printf ("STR1 's specified substring equals substring of the first 2 characters of the specified string \ n");
	else printf ("str1 specified substring is not equal to the substring of the first 2 characters of the specified string \ n");
	
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.