Answers for "string compare"

5

how to compare strings in c++

// 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 an apple\n";

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

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

  return 0;
}
Posted by: Guest on July-14-2020
1

string compare java

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false
Posted by: Guest on September-18-2020
0

String compare

Input  : ABCD, XYZ
Output : ABCD is not equal to XYZ
         XYZ is greater than ABCD

Input  : Geeks, forGeeks
Output : Geeks is not equal to forGeeks
         forGeeks is greater than Geeks
Posted by: Guest on November-09-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language