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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:14:01+00:00 2026-05-26T06:14:01+00:00

If you want to know if a string starts with another, how would you

  • 0

If you want to know if a string starts with another, how would you do that in C++/STL? In Java there is String.startsWith, Python also has string.startwith, STL does not have a direct method for it. Instead, there are std::string::find and std::string::compare. Until now I used both methods, mostly depending on my current mood:

if ( str1.compare( 0, str2.length(), str2 ) == 0 )
    do_something();
if ( str1.find(str2) == 0 )
    do_something();

Of course, you could also do str.substr(0,str2.length()) == str2, maybe there are still some other ways do achieve the same. find is a bit handier than compare, but I have seen more people recommending compare that find.

But which one is preferred? Is there a performance difference? Is it implementation-dependent (GCC, VC++, etc)?

  • 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-26T06:14:02+00:00Added an answer on May 26, 2026 at 6:14 am

    The disadvantage of find is that if str1 is long, then it will pointlessly search all the way through it for str2. I’ve never noticed an optimizer being smart enough to realise that you only care whether the result is 0 or not, and stop searching after the start of str1.

    The disadvantage of compare is that you need to check that str2.length() is no greater than str1.length() (or catch the resulting exception and treat it as a false result).

    Disappointingly, the closest thing to what you want in the standard library is std::strncmp (and of course you need to use c_str() with that), hence the need for boost::starts_with or your own equivalent which includes the bounds checks.

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

Sidebar

Related Questions

I would like to know how to load string resource from another class library.
want to know why String behaves like value type while using ==. String s1
i'm amol kadam,I want to know how to split string in two part.My string
I just want to know the difference between List<string> lst = new List() and
i want to remove a particular #keyword from a string. i dont know its
I want to know that can mouse click event detects colors or even strings.
I'm making a chat responder for a game and i want know if there
i want to show a PD when my activity A starts another activity B.
I have a submit button that has got a dynamically generated id. I want
I need to build a regex. the string i want to match always starts

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.