Home > Enterprise >  Is there a function to concatenate two char16_t
Is there a function to concatenate two char16_t

Time:03-02

There is the function wcsncat_s() for concatenating two wchar_t*:

errno_t wcsncat_s( wchar_t *restrict dest, rsize_t destsz, const wchar_t *restrict src, rsize_t count );

Is there an equivalent function for concatenating two char16_t?

CodePudding user response:

Not really.

On Windows, though, wchar_t is functionally identical to char16_t, so you could just cast your char16_t* to a wchar_t*.

Otherwise you can do it simply enough by writing yourself a function for it.

CodePudding user response:

You could use std::u16string if you want something portable.

std::u16string str1(u16"The quick brown fox ");
std::u16string str2(u16"Jumped over the lazy dog");

std::u16string str3 = str1 str2;  // concatenate

const char16_t* psz = str3.c_str();

The validity of psz lasts as long as str3 doesn't go out of out scope.

But the more portable and flexible solution is to just use wchar_t everywhere (which is 32-bit on Mac). Unless you are explicitly using 16-bit char strings (perhaps for a specific UTf61 processing routine), it's easier to just keep your code in the wide char space. Plays nicer with native APIs and libraries on Mac and Windows.

  • Related