Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you concatenate two wchar_t* together?

Tags:

c++

I have a base wchar_t* and I'm looking to append another one onto the end. How do I do it? I cannot use deprecated functions as I am treating warnings as errors.

like image 282
Chad Avatar asked Dec 06 '09 17:12

Chad


People also ask

How do I concatenate two characters to a string?

The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function. In the above example, we have declared two char arrays mainly str1 and str2 of size 100 characters.

Which is the correct way to concatenate 2 strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator.

How do you concatenate Wchar?

Description. The wcscat() function appends a copy of the string pointed to by string2 to the end of the string pointed to by string1. The wcscat() function operates on null-ended wchar_t strings. The string arguments to this function should contain a wchar_t null character marking the end of the string.

What is concatenation with example?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball".


2 Answers

Why not use a std::wstring in the first place:

wchar_t *ws1 = foo(), *ws2 = bar();
std::wstring s(ws1);
s += std::wstring(ws2);
std::wcout << s << std::endl;

If needed, std::wstring::c_str() gives you access to the result as a const wchar_t*.

like image 127
Georg Fritzsche Avatar answered Oct 23 '22 17:10

Georg Fritzsche


 #include <wchar.h>

 wchar_t *wcsncat(wchar_t *ws1, const wchar_t *ws2, size_t n); 

The wcsncat() function appends no more than the first n characters of the string pointed to by ws2 to the end of the string pointed to by ws1. If a NULL character appears in ws2 before n characters, all characters up to the NULL character are appended to ws1. The first character of ws2 overwrites the terminating NULL character of ws1. A NULL terminating character is always appended to the result, and if the objects used for copying overlap, the behavior is undefined.

ws1

Is the null-terminated destination string.

ws2

Is the null-terminated source string.

n

Is the number of characters to append.

like image 29
Brendan Lesniak Avatar answered Oct 23 '22 18:10

Brendan Lesniak