none
[UWP][C++] String Format RRS feed

  • Question

  • Hi, what is the fastest way to build an C++/CX String^ from the concatenation of these three functions, thanks.

    const wchar_t* GetMediaName( );
    __int64 GetTimeStamp( );
    __int64 GetUniqueID( );

    Thursday, June 30, 2016 4:27 AM

All replies

  • String constructor for the wchar_t pointer

    __int64 presents itself with a toString method.

    then you use the + operator to concat them.

    Thursday, June 30, 2016 4:59 AM
  • Thanks, this is the easiest way but because a String^ is immutable, concatenation operations involve the destruction of the original String and the creation of a new object.

    I just discovered the Platform::StringReference class that allow to pass a const wchar_t* across the ABI boundary to a function accepting a String^ without making a copy.
    Thursday, June 30, 2016 10:22 PM
  • Hi t-n-x,
    We can convert wchar_t* and __int64 to string. Then we can use basic_string::append to add the other string to the first string.
    For example:

    #include <sstream>
    using namespace std;
    const wchar_t* GetMediaName = L"Hello World";
    wstring ws(GetMediaName);
    string str(ws.begin(), ws.end());
    stringstream sstr;
    stringstream sstr2;
    __int64 GetTimeStamp = 111;
    __int64 GetUniqueID = 222;
    sstr << GetTimeStamp;
    string str2 = sstr.str();
    sstr2 << GetUniqueID;
    string str3 = sstr2.str();
    str.append(str2);
    str.append(str3);
    

    Best Regards,
    Jayden Gu

    • Proposed as answer by Jayden Gu Thursday, July 7, 2016 2:34 AM
    • Marked as answer by Barry WangModerator Friday, July 8, 2016 9:01 AM
    • Unmarked as answer by t-n-x Saturday, December 10, 2016 12:19 PM
    Thursday, July 7, 2016 2:33 AM