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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:32:27+00:00 2026-05-27T01:32:27+00:00

I have a std::string such as 20040531 , I want to format this as

  • 0

I have a std::string such as 20040531, I want to format this as 2004.05.31.

Apart from the straight forward way of doing an std::insert at respective locations, is there a better way to do this using Boost?

PS. I cannot use other Boost calls to get date/time as this string is returned via a custom API. So this question is reduced to basic string formatting which may not sound exciting, but I am trying to learn Boost.

  • 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-27T01:32:28+00:00Added an answer on May 27, 2026 at 1:32 am

    You could use boost::format…

    #include <string> 
    #include "boost/format.hpp"
    #include <iostream>
    
    int main()
    {
        std::string a("20040531");
        std::cout << boost::format("%1%.%2%.%3%") 
                        % a.substr(0,4) % a.substr(4,2) % a.substr(6,2);
    }
    

    You specifically asked about doing this using Boost, but if you wanted to do this in C++ without introducing a dependency on Boost then you could just use a stream to achieve the same thing:

    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::stringstream s;
        std::string a("20040531");
        s << a.substr(0,4) << '.' << a.substr(4,2) << '.' << a.substr(6,2);
        std::cout << s.str();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have some code that looks like: static const std::string and( AND ); This
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have unsigned char* , want to convert it to std::string . Can you
I have this puny program. #include <iostream> #include <string> int main() { std::string st
So I want to create a simple map std::map<T1, std::string> and I have a
So I have such function: boost::shared_ptr<my_class> get_class_by_name(std::string name) { typedef std::map<boost::shared_ptr<my_class>, my_description> map_t; BOOST_FOREACH(map_t::value_type
I have an istringstream object with string of format STRING,INT,INT,INT eg. name,20,30,40 I want
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container

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.