-4

One of my team (a C++ developer) tells me that it is 'very complicated and messy' to compare two strings for equality, because they could each be of several different string types. I am stunned that in 2014 this might still be the case (in C#, VB.Net and most other langauges I am familiar with, comparing strings is a trivial step). Is there not a freely available library that handles this type of detail, so that two strings can easily be compared for equality without worrying about their string types?

haughtonomous
  • 4,602
  • 11
  • 34
  • 52
  • 1
    What is your definition of string equality? Is it "all bytes are equal", is it "all glyphs are equal", is it something different? – dyp Aug 04 '14 at 11:41
  • possible duplicate of [How to compare the string in C++?](http://stackoverflow.com/questions/9785386/how-to-compare-the-string-in-c) – Leo Chapiro Aug 04 '14 at 11:41
  • 4
    [`std::string::operator==`](http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp)? – Some programmer dude Aug 04 '14 at 11:41
  • or what about [string::compare](http://www.cplusplus.com/reference/string/string/compare/)? – Johan Hjalmarsson Aug 04 '14 at 11:43
  • 3
    @duDE I disagree that it is a duplicate of *that* particular question. That question seems to be about a specific kind of string containing dates. – dyp Aug 04 '14 at 11:43
  • 1
    *"because they could each be of several different string types"* Historical reasons. Different libraries use different string types; legacy code uses some kind of C-string etc. Even if everyone agreed to use the same encoding, C++ Standard Library types are not fit for interfaces on all platforms, since their layout is not standardized. – dyp Aug 04 '14 at 11:47
  • 1
    add a utility method to convert these "other" string types to std strings, then you may use ::compare or ::operator== – paulm Aug 04 '14 at 11:48
  • What kind of strings are you having problems with? – juanchopanza Aug 04 '14 at 11:53
  • To clarify: this is legacy code that has 'string' variables of varying types (char, bstr, etc). The task is to compare two such variables to see if they hold the same 'string'. ie two variables of different types may in their own way contain strings representing 'Fred' or maybe 'Joe', and I want to test if they have the same 'value' (even if they are encoded differently, or whatever). The actuall string types are a given, so I can't change them all to std::string or whatever. – haughtonomous Aug 04 '14 at 11:53
  • "I am stunned that in 2014 this might still be the case" - it isn't. We've had `std::string` for decades. Use that for all your internal logic, and only convert to or from weird types when you need them to support a particular weird API. – Mike Seymour Aug 04 '14 at 11:54
  • "Use std::string for all your internal logic" Thanks, but that is converting a relatively small problem into a large problem - this is a large codebase that will require a lot of modification to do that. – haughtonomous Aug 04 '14 at 11:56
  • @MikeSeymour and call a UTF-8 transformation function every time you need to call a WinAPI function ... ugh (not that I really have a better idea) – M.M Aug 04 '14 at 11:59
  • possible duplicate of [how can I compare utf8 string such as persian words in c++?](http://stackoverflow.com/questions/7141417/how-can-i-compare-utf8-string-such-as-persian-words-in-c) – Bull Aug 04 '14 at 12:14
  • -1 because you want to ask, but don't want to use google... – ST3 Aug 05 '14 at 18:04
  • Friend, I don't know who you are but you clearly have time to waste. I came to Stackoverflow because I want to ask the people that are most likely to know the answer, rather than wade through hundreds of pages which may or may not contain a reliable answer. Your comment basically suggests that Stackoverflow is redundant, which is an interest insight. Thanks to everyone else who helped. – haughtonomous Aug 07 '14 at 08:06

1 Answers1

0

Your question isn't very well formulated, but I think you have an issue with different UTFs. So even same bytes may mean different things.

If this so, you need to have the same base, e.g. UTF-8, you didn't included platform, but it looks like Windows, so you could use WideCharToMultiByte to convert data.

Also there some others ways to do that if this doesn't suit your needs, so use Google

ST3
  • 8,826
  • 3
  • 68
  • 92
  • Sorry, I meant Windows (Visual C++ specifically). I purposely didn't use Google because I don't want to invite millions of targeted spam emails inviting me on a C++ course :-) – haughtonomous Aug 05 '14 at 15:38