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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:02:14+00:00 2026-06-12T23:02:14+00:00

I have this string: std::string date = 20121020; I’m doing: std::cout << ** Date:

  • 0

I have this string:

std::string date = "20121020";

I’m doing:

std::cout << "** Date: " << date << "\n";
std::cout << "Year: " << date.substr(0,4) << "\n";
std::cout << "Month: " << date.substr(4,6) << "\n";
std::cout << "Day: " << date.substr(6,8) << "\n";

But im getting:

** Date: 20121020
Year: 2012
Month: 1020
Day: 20

Notice that month should be 10, not 1020. Positions are correct, tried everything, that is it failing?

  • 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-06-12T23:02:15+00:00Added an answer on June 12, 2026 at 11:02 pm
    std::cout << "Month: " << date.substr(4,6) << "\n";
    

    The second argument is wrong. You are specifying, “Give me as substring of 6 characters, starting at position 4.”

    You probably want:

    std::cout << "Month: " << date.substr(4,2) << "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
unordered_map<std::string, std::string>* Accounts; I have this code up there to initialize from a pointer,
unordered_map<std::string,unordered_map<std::string, std::string> >* storing_vars; I have this variable in the scope declared in the
I have this in furniture.h: #include <iostream> #include <string> using namespace std; class Furniture
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have these variables: boost::regex re //regular expression to use std::string stringToChange //replace this
I have some code that looks like: static const std::string and( AND ); This
I have this function that converts an integer to a std::string: std::string intToStr(const int
Suppose I have this function: std::string Func1(std::string myString) { //do some string processing std::string
I have this code: std::string name = kingfisher; char node_name[name.size()+1]; strcpy(node_name,name.c_str()); node_name[name.size()] = '\0';

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.