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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:27:20+00:00 2026-05-28T20:27:20+00:00

I am using the substr() function, however it’s not working. My code is below

  • 0

I am using the substr() function, however it’s not working. My code is below

std::string s1 = ".V/123\n"
          ".V/233\n";

std::string ss;
if(s1.substr(0,3) == ".V/")
{
    ss = s1.substr(3);

    std::cout << ss;
}  else {
    std::cout << "INCORRECT" << std::endl;
}

The output is 123.V/123

Shouldn’t it be:

123
123

Could someone tell me where I am going wrong please?

  • 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-28T20:27:22+00:00Added an answer on May 28, 2026 at 8:27 pm

    Your string contains 2 lines. Doing “.V/123\n” “.V/223\n” will do the same as “.V/123\n.V/223\n”. Either split that up into separate variables or an array. What your substr(3) is doing is extracting all the characters in the string from the 4th character to the end, so ‘ss’ is getting set to “123\n.V/223\n”. This is what you’re seeing.

    What you want is something like this

      std::string s1[2] = { ".V/123\n", ".V/233\n"};
    
      std::string ss;
      for (int i = 0; i < 2; ++i) {
        if (s1[i].substr(0,3) == ".V/") {
          ss = s1[i].substr(3);
    
          std::cout << ss;
        }  else {
          std::cout << "INCORRECT" << std::endl;
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using substr to trim the first 100 characters from the string. however
If i type www.google.com or http://www.google.com using auto_link() function will add link correctly.. However,
I'm using the following code on my WordPress install: function add_glossary_links($content) { global $wpdb,
I'm using the getExcerpt() function below to dynamically set the length of a snippet
I am trying to extract substring from string gettting from mysql database using substr
I am using the following code: PHP: // Generate Guid function NewGuid() { $s
I was using the strrchr PHP function with substr and strrpos to find the
I'm using the substr() function to limit the characters in strings. but sometimes, the
I am slicing unicode string with diacritics using mb_substr function but it works as
I'm using following code to register a function for click event of all SimpleButton

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.