How do I format a std:string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How do I format a std:string?

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    How do I format a std:string?

    How do I format a std:string?
    I mean like VB String Format function.

    for example I need to ensure that a string will be at least 3 chars long/6 chars long (suppose to be generic, of course)

    Thanks
    avi123

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    There is no direct format specification possible for a string. In this case use stringstream to format the string and then pass it to a string...
    Code:
    #include <string>
    #include <sstream>
    #include <iomanip>
    
    int main()
    {
      std::stringstream strsTemp;
    
      strsTemp << std::setw(3) << 2 << std::endl;
    
      std::string strString = strsTemp.str();
    
      return 0;
    }
    There exists several format specification...take a look at
    • setw
    • setfill
    • left
    • right

    These are basically the ones you would need here based on your description...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center