How to check if a string is equal to a string literal
Asked Answered
C

5

29

I wanted to compare a string to a string literal; something like this:

if (string == "add")

Do I have to declare "add" as a string or is it possible to compare in a similar way?

Countercurrent answered 3/6, 2011 at 3:10 Comment(2)
What is the type of string? Is it a C++ std::string object, or simply a const char*?Icarus
@Icarus awnser is good. Also worth mentioning that a string or char sequence literal should be enclosed with " (double quotes), single quotes are for single char literals.Loxodromic
I
66

In C++ the std::string class implements the comparison operators, so you can perform the comparison using == just as you would expect:

if (string == "add") { ... }

When used properly, operator overloading is an excellent C++ feature.

Icarus answered 3/6, 2011 at 3:14 Comment(6)
Well now I'm really confused. If string is a std::string object, this will work, but then strcmp() would not have worked. Which is it? I'm dying to know! :)Icarus
But it should be noted, that the comparison will yield a different result if "string" is char* !Gervase
e.James: if "string" is std::string, then if( strcmp(string.c_str(),"add")==0 ) {...} would work.Gervase
@frunsi: Yes, but that would be really unnecessary. I was referring to Anon's comment in Christopher Armstrong's answer (where he says that if (strcmp(string, "add") == 0) { ... } worked.Icarus
@frunsi: I am worried that string is a char*, in which case this is the wrong answer. I was hoping Anon would clarify that.Icarus
What if string is a char* ? What should I do?Hight
M
7

You need to use strcmp.

if (strcmp(string,"add") == 0){
    print("success!");
}
Mulligan answered 3/6, 2011 at 3:12 Comment(4)
Note: strcmp() is a C function which takes pointers to const char* as its arguments. It works in this case because the std::string object is converted back into a const char*, but this is not the best way to do things in C++Icarus
@e.James: No, std::string does not implicitly convert to const char*, you need to call s.c_str() for that. But according to the question "without defining either one as a string", string isn't a std::string to begin with.Adria
@Ben Voigt: Ah, yes you are right about the down-conversion. My mistake.Icarus
In C code you should prefer strncmp() over strcmp(), which is safer solution.Emelina
B
0

Option A - use std::string

std::string has an operator overload that allows you to compare it to another string.

std::string string = "add";
if (string == "add") // true

Option B - use std::string_view(C++17)

If one of the operands isn't already a std::string or std::string_view, you can wrap either operand in a std::string_view. This is very cheap, and doesn't require any dynamic memory allocations.

#include <string_view>
// ...

if (std::string_view(string) == "add")
// or
if (string == std::string_view("add"))
// or
using namespace std::string_literals;
if (string == "add"sv)

Option C - use strcmp(compatible with C)

If neither of those options is available, or you ned to write code that works in both C and C++:

#include <string.h>
// ...

const char* string = "add";
if (strcmp(string, "add") == 0) // true
Buckra answered 7/9, 2023 at 14:10 Comment(0)
O
-1

You could use strcmp():

/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szKey[] = "apple";
  char szInput[80];
  do {
     printf ("Guess my favourite fruit? ");
     gets (szInput);
  } while (strcmp (szKey,szInput) != 0);
  puts ("Correct answer!");
  return 0;
}
Oleo answered 3/6, 2011 at 3:13 Comment(2)
This will work (because C++ is backwards-compatible with C) but it is not the best way to do things in C++Icarus
This also has a buffer overflow issue, there is no length checking on szInputRoper
A
-1

We use the following set of instructions in the C++ computer language.

Objective: verify if the value inside std::string container is equal to "add":

if (sString.compare(“add”) == 0) { //if equal
    // Execute
}
Albanese answered 24/7, 2021 at 23:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.