string manipulation?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: string manipulation?

  1. #1
    Join Date
    Sep 2003

    string manipulation?


    I was wondering if there is a function that does the follwoing manipulation over a string containing a number:

    Input :
    std::string sMyNumber = "1234.56"


    in short: adding , in long in numbers


  2. #2
    Join Date
    Sep 2003
    well I did some research, and found out that there isn't such a function in stl

    so I wrote these function, what do you think?

    string CGeneral::AddComma(string* psStr)

    string sTmp = *psStr;

    string::size_type pos = sTmp.find('.');

    //check if the string holds decimal point
    if (pos != string::npos)
    sTmp = sTmp.substr(0,pos);

    reverse(sTmp.begin(), sTmp.end());

    string sResult;

    while(sTmp.length() > 3)
    //group them, 3 at a time
    sResult += sTmp.substr(0,3) + _T(",");
    sResult += sTmp;

    reverse(sResult.begin(), sResult.end());

    //add the .### part
    if (pos != string::npos)
    sResult += psStr->substr(pos,psStr->length());

    return sResult;

  3. #3
    Join Date
    Apr 1999
    Originally posted by avi123
    well I did some research, and found out that there isn't such a function in stl
    Your solution will look wierd for countries that use "." or spaces for the thousands separator (this is directed to the OP as well). At the very least, you should get the locale information to determine what are the different separators for a number.

    Yves the moderator did some more research, and you can achieve it using the C++ library. An example from MSDN:
    // num_put_put.cpp
    // compile with: /EHsc
    #include <locale>
    #include <iostream>
    #include <sstream>
    using namespace std;
    int main( )
       locale loc( "german_germany" );
       basic_stringstream<char> psz2;
       ios_base::iostate st = 0;
       long double fVal;
       cout << "The thousands separator is: " 
            << use_facet < numpunct <char> >(loc).thousands_sep( ) 
            << endl;
       psz2.imbue( loc );
       use_facet < num_put < char > >
          ( loc ).put(basic_ostream<char>::_Iter(psz2.rdbuf( ) ),
                        psz2, st, fVal=1000.67);
       if ( st & ios_base::failbit )
          cout << "num_put( ) FAILED" << endl;
          cout << "num_put( ) = " << psz2.rdbuf( )->str( ) << endl;
    BTW avi123, not everything is STL. There are many parts of C++ library that have nothing to do with STL, as Yves solution demonstrates. Locales are not part of STL. So if you are looking for a solution, and you turn to your STL reference, you won't find it. If you turn to the C++ library reference referring to locale information, then you will find what you're looking for.


    Paul McKenzie

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 survey!

HTML5 Development Center