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 886683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:06:33+00:00 2026-05-15T13:06:33+00:00

I am using std::string ‘s find() method to test if a string is a

  • 0

I am using std::string‘s find() method to test if a string is a substring of another. Now I need case insensitive version of the same thing. For string comparison I can always turn to stricmp() but there doesn’t seem to be a stristr().

I have found various answers and most suggest using Boost which is not an option in my case. Additionally, I need to support std::wstring/wchar_t. Any ideas?

  • 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-15T13:06:34+00:00Added an answer on May 15, 2026 at 1:06 pm

    You could use std::search with a custom predicate.

    #include <locale>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    // templated version of my_equal so it could work with both char and wchar_t
    template<typename charT>
    struct my_equal {
        my_equal( const std::locale& loc ) : loc_(loc) {}
        bool operator()(charT ch1, charT ch2) {
            return std::toupper(ch1, loc_) == std::toupper(ch2, loc_);
        }
    private:
        const std::locale& loc_;
    };
    
    // find substring (case insensitive)
    template<typename T>
    int ci_find_substr( const T& str1, const T& str2, const std::locale& loc = std::locale() )
    {
        typename T::const_iterator it = std::search( str1.begin(), str1.end(), 
            str2.begin(), str2.end(), my_equal<typename T::value_type>(loc) );
        if ( it != str1.end() ) return it - str1.begin();
        else return -1; // not found
    }
    
    int main(int arc, char *argv[]) 
    {
        // string test
        std::string str1 = "FIRST HELLO";
        std::string str2 = "hello";
        int f1 = ci_find_substr( str1, str2 );
    
        // wstring test
        std::wstring wstr1 = L"ОПЯТЬ ПРИВЕТ";
        std::wstring wstr2 = L"привет";
        int f2 = ci_find_substr( wstr1, wstr2 );
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Up until now I have been using std::string in my C++ applications for embedded
I'm using std::string and need to left pad them to a given width. What
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
I'm trying to get a std::string from my database using mysql connexion Here is
Using the STL C++ hash_map... class MyKeyObject { std::string str1; std::string str2; bool operator==(...)
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=
How can i create a boost::format type formatter object using the character-string or std::string
Suppose I have the following: struct Person { std::string mName; Birthday mBirthday; }; using
I'm looking for a simple way to tokenize std::string input without using non default
I'm using boost uuid to generate session ids. std::string SessionGenerator::generate() { boost::uuids::uuid id =

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.