Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:23:46+00:00 2026-05-27T22:23:46+00:00

I am comparing two strings, str1 and str2, using the string.compare function in #string.

  • 0

I am comparing two strings, str1 and str2, using the string.compare function in #string. Is there a way to force the class to think that '-' is the equivalent of ' '. Looking at the member functions of char_traits I thought that the .assign would allow me to accomplish this but it is acting as if I am saying, str1='-'; or str1=' ';. I would prefer not to rewrite my own string handling class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:23:46+00:00Added an answer on May 27, 2026 at 10:23 pm

    There are several possibilities, depending whether you want this behavior:

    • once
    • encoded in the class

    If you want to behavior once: simply use your own (custom) algorithm:

    bool isSpace(char i) { return i == '-' or i == ' '; }
    
    int compare(std::string const& left, std::string const& right) {
      typedef std::string::const_iterator ConstIterator;
      typedef std::pair<ConstIterator, ConstIterator> Result;
    
      size_t const size = std::min(left.size(), right.size());
    
      Result const r = std::mismatch(left.begin(),
                                     left.begin() + size,
                                     right.begin(),
                                     [](char a, char b) {
                                       return a == b or (isSpace(a) and isSpace(b));
                                     });
    
      if (r.first == left.begin() + size) { // equal up til the end, shorter wins
        return left.size() < right.size() ? -1 :
              (left.size() == right.size() ? 0 : 1);
      }
    
      // not equal until the end
      return *r.first < *r.second ? -1 : 1;
    }
    

    If this behavior need be encoded within the class itself, you need to use basic_string and provide a custom trait class.

    The traits class provides a static int compare ( const char_type* s1, const char_type* s2, size_t n); function that is used by std::string::compare under the hood.

    So for example:

    struct MyTraits: char_traits<char> // too lazy to reimplement everything
    {
      static int compare(const char_type* s1, const char_type* s2, size_t n);
      // definition can be trivially derived from the above version
    };
    
    typedef std::basic_string<char, MyTraits> MyString;
    

    Of course, MyString is then completely incompatible with other std::string.


    Frankly, if you can, simply “normalize” your string and decide whether you’ll use ‘-‘ or ‘ ‘. It will make your life easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a fastest way to compare two strings (using the space for a
I'm comparing two strings like so: <?php $Str1 = '111122223333444455556666'; $Str2 = '111122223333444455557777'; if($Str1
I've had some success comparing strings using the PHP levenshtein function. However, for two
When comparing two strings how to avoid checking if a string is of different
I have an android app. in that I am comparing two strings from the
I have two strings that I am comparing in a unit test. When I
I know that == has some issues when comparing two Strings . It seems
I am trying two compare two strings in JavaScript. But I guess there is
When comparing two strings in c# for equality, what is the difference between InvariantCulture
I am comparing between two tables first column each. If there is find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.