System.UnicodeString.printf (C++)

From RAD Studio Code Examples
Jump to: navigation, search


The following code example illustrates the usage of the printf function on Windows and OS X platforms.


#include <System.hpp>
#include <stdio.h>

int main(int argc, char* argv[]) {
  /* This works on both Windows and OSX */
  wchar_t* name1 = L"John";
  System::UnicodeString s1;
  s1.printf(L"Hello %ls", name1);

  System::UnicodeString name2 = L"John";
  System::UnicodeString s2;
#if defined(_WIN32)
  /* This, however, only works on Windows */
  s2.printf(L"Hello %ls", name2.c_str());
  printf("%ls", s2.c_str());
#elif defined(__APPLE__)
  /* To printf the contents of a UnicodeString, convert it
     to UTF8 first and use %s instead of %ls */    
  s2.printf(L"Hello %s", System::UTF8String(name2).c_str());
  printf("%s", System::UTF8String(s2).c_str());

  return 0;

Note: For more details, see Unicode Output Format Specifiers.