Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I substring a TCHAR

Tags:

c++

windows

tchar

I have a TCHAR and value as below:

TCHAR          szDestPathRoot[MAX_PATH]="String This";

Now I want the 1st three character from TCHAR , like below:

szDestPathRoot.substring(0,2);

How can I do this.

like image 354
Simsons Avatar asked Oct 28 '10 04:10

Simsons


2 Answers

TCHAR[] is a simple null-terminated array (rather than a C++ class). As a result, there's no ".substring()" method.

TCHAR[] (by definition) can either be a wide character string (Unicode) or a simple char string (ASCII). This means there are wcs and str equivalents for each string function (wcslen() vs strlen(), etc etc). And an agnostic, compile-time TCHAR equivalent that can be either/or.

The TCHAR equivalent of strncpy() is tcsncpy().

Final caveat: to declare a TCHARliteral, it's best to use the _T() macro, as shown in the following snippet:

TCHAR szDestPathRoot[MAX_PATH] = _T("String This");
TCHAR szStrNew[4];
_tcsncpy (str_new, szTestPathRoot, 3);

You may find these links to be of interest:

  • http://msdn.microsoft.com/en-us/library/xdsywd25%28VS.71%29.aspx
  • http://www.i18nguy.com/unicode/c-unicode.html
  • http://msdn.microsoft.com/en-us/library/5dae5d43(VS.80).aspx (for using the secure _tcsncpy_s)
like image 105
paulsm4 Avatar answered Sep 26 '22 09:09

paulsm4


TCHAR szDestPathRoot[MAX_PATH]="String This";
TCHAR substringValue[4] = {0};
memcpy(substringValue, szDestPathRoot, sizeof(TCHAR) * 3);
like image 38
tidwall Avatar answered Sep 26 '22 09:09

tidwall