none
Format String in C++ RRS feed

  • Question

  • Hi All,

            I am not using MFC in my project and also runing with UNICODE. So I can't use "CString" class in my project and CString provide many functions(Format(), reverseFind(), Left(), etc) but I need "Format()" like function for for fomating the strings.

    I am using "ICU" APIs and it provide "UnicodeString" class which can use on place of "CString", but there are few function not finding. If any one using "ICU" APIs then please tell me about "Format()" function or "Format()) like functiopn.

    Wednesday, March 7, 2012 6:44 AM

Answers

  • I think that you can replace CString::Format with your own function that uses vswprintf or StringCchVPrintfW. As a destination buffer use the value returned by UnicodeBuffer::getBuffer(...). If the buffer is insufficient, then make a loop that tries a larger one. Then call UnicodeBuffer::releaseBuffer(-1).

    If you have the sources of ATL and MFC, then see how Format is done.

    • Edited by Viorel_MVP Wednesday, March 7, 2012 7:57 AM
    • Marked as answer by Rob Pan Tuesday, March 13, 2012 9:24 AM
    Wednesday, March 7, 2012 7:41 AM

All replies

  • I think that you can replace CString::Format with your own function that uses vswprintf or StringCchVPrintfW. As a destination buffer use the value returned by UnicodeBuffer::getBuffer(...). If the buffer is insufficient, then make a loop that tries a larger one. Then call UnicodeBuffer::releaseBuffer(-1).

    If you have the sources of ATL and MFC, then see how Format is done.

    • Edited by Viorel_MVP Wednesday, March 7, 2012 7:57 AM
    • Marked as answer by Rob Pan Tuesday, March 13, 2012 9:24 AM
    Wednesday, March 7, 2012 7:41 AM
  • CString is no longer tied to MFC.

    Non-MFC code can still use CString by including the altstr.h header file.

    You can also use the std::wstringstream object to do similar formatting.


    «_Superman_»
    Microsoft MVP (Visual C++)

    Polymorphism in C

    Wednesday, March 7, 2012 12:00 PM