Back | Reverse | Quick Reply | Post Reply |

C++ string equals method?
Link | by qwe on 2006-01-14 16:07:07
ya, I'm a java programmer that is starting to learn C++. what is the equals method for strings for C++?
ex. in java the .equals method is built in for character comparison (not memory comparison) is there one for C++ or do you have to write your own or paste it from somewhere?

Re: C++ string equals method?
Link | by gendou on 2006-01-14 16:45:28
you can use strcmp() to check if 2 strings are the same or not


Re: C++ string equals method?
Link | by hunterjamaica on 2006-01-26 04:24:36 (edited 2006-01-26 04:26:39)
In C:
#include string.h

char str1[] = "foo";
if(strcmp(str1,"foo") == 0) {
//str1 equals "foo"
}
if(strcmp(str1,"bar")) {
//str1 does not equal "bar"
}

In C++:
#include string

std::string str1 = "foo";
if(str1 == "foo") {
//str1 equals "foo"
}
if(str1 != "bar") {
//str1 does not equal "bar"
}

thats it nice and easy

edit: remember to put the greater than and less than symbols after #include

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0024 seconds at 2024-12-27 14:14:00