5

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.

Simsons
  • 12,295
  • 42
  • 153
  • 269
  • Hello, you should use `MAX_PATH + 1` to prevent overflows. – Benoit Oct 28 '10 at 04:51
  • @Benoit: `MAX_PATH` is 260, which includes the null termination length. (The maximum supported path length is 255 characters) See http://msdn.microsoft.com/en-us/library/aa365247.aspx – Billy ONeal Oct 28 '10 at 05:12
  • 4
    Your string literal should be enclosed in the `_T()` macro. That will make it a wide string literal in unicode builds. – Alex Jasmin Oct 28 '10 at 05:17
  • I did not downvote, but I would guess the downvote would be because the question reflects a non understanding of the C language itself. You should probably get your hands on a C book and learn C before attempting Win32 stuff. – Billy ONeal Oct 29 '10 at 14:55

4 Answers4

13

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:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
4
TCHAR szDestPathRoot[MAX_PATH]="String This";
TCHAR substringValue[4] = {0};
memcpy(substringValue, szDestPathRoot, sizeof(TCHAR) * 3);
tidwall
  • 6,881
  • 2
  • 36
  • 47
  • 2
    @Blindy: memcpy is an ansi c method that takes a const as the second param. How does this destroy the original string? – tidwall Oct 28 '10 at 06:33
1

This is somewhat ugly but if you know for sure that:

  1. The string holds at least 4 TCHAR (3 chars plus the terminating NUL)
  2. The content of the string can be modified (which is the case in your example).
  3. You don't have to keep the original string intact

You could just put a terminating NUL at the 4th position to make the string 3 char long.

szDestPathRoot[3] = _T('\0');

Note that this operation is destructive to the original string

You should really be using a string class in C++ code though.

Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
  • @Alexandre Jasmin , I don't understand how does this substring the szDestPathRoot . I need Str from the above assignment – Simsons Oct 28 '10 at 04:51
  • @Subhen: It has a bug (it should be placing the NULL at position 4), but what it does is limit the length of the string to x-1 number of characters. The code places the string ending character (NULL) at position three, and therefore the length of the string in question becomes two. – Billy ONeal Oct 28 '10 at 05:15
  • 1
    @Billy ONeal s[0]='S'; s[1]='T'; s[2]='R'; s[3]='\0'. I count three chars. – Alex Jasmin Oct 28 '10 at 05:24
  • @Alexandre: No, the length of the string is 2. It occupies 3 bytes of memory, but the null is not part of the string. – Billy ONeal Oct 28 '10 at 21:10
  • @Billy, I think I see why you're confused. That's an assignment statement, not an array declaration. – Mark Ransom Oct 28 '10 at 21:26
  • You should note that this is destructive to the original string. – Mark Ransom Oct 28 '10 at 21:28
  • @Mark: Oops. I'll get surgury to remove the foot next week :) – Billy ONeal Oct 29 '10 at 13:34
1

As you have tagged your question with "C++" you can use the string classes of the std library:

std::wstring strDestPathRoot( _T("String This") );
strDestPathRoot.substr( 0, 2 );
Flinsch
  • 4,296
  • 1
  • 20
  • 29
  • I do not want to convert to string – Simsons Oct 28 '10 at 07:21
  • Then just use `strncpy` or `strncpy_s`. But, however, why not using std's string classes? – Flinsch Oct 28 '10 at 07:30
  • Two nitpicks. 1. `std::wstring` is a string of `wchar_t` not `TCHAR`. With `std::wstring` don't use the `_T()` macro just prefix the literal with an `L`. 2. As @user421195 mentioned the `TCHAR` version of `strncpy()` is `tcsncpy()` – Alex Jasmin Oct 28 '10 at 07:48
  • 6
    If you're using `TCHAR`, it should be `std::basic_string`, not `std::wstring`. – jalf Oct 28 '10 at 10:07