Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Truncate a LPCTSTR

25 views
Skip to first unread message

Luigino

unread,
Mar 5, 2010, 10:42:53 AM3/5/10
to
Hello everyone,

I'm trying to truncate with a certain LPCTSTR string to n position and
get another LPCTSTR variable truncated by that n position....

Is it possible? Any hint?...

Thanks
Ciao
Luigi

Luigino

unread,
Mar 5, 2010, 10:48:00 AM3/5/10
to

Scott McPhillips [MVP]

unread,
Mar 5, 2010, 11:09:40 AM3/5/10
to
The 'C' in LPCTSTR stands for 'const', so you can't change that string.
Copy the n chars that you want into a new string.


"Luigino" <npu...@rocketmail.com> wrote in message
news:6e2fc0ea-6206-4e9d...@j27g2000yqn.googlegroups.com...

--
Scott McPhillips [VC++ MVP]

Giovanni Dicanio

unread,
Mar 5, 2010, 11:24:02 AM3/5/10
to

"Luigino" <npu...@rocketmail.com> ha scritto nel messaggio
news:a6584f98-f862-4fb9...@b7g2000yqd.googlegroups.com...

> I'm trying to truncate with a certain LPCTSTR string to n position and
> get another LPCTSTR variable truncated by that n position....

In addition to Scott's correct post, you may want to use CString::Left
method:

http://msdn.microsoft.com/en-us/library/sw4as8az(VS.80).aspx

Giovanni

nexolite

unread,
Mar 5, 2010, 11:52:02 AM3/5/10
to
you can do this in the following way:

LPCTSTR str = _T("My Test String");
CString cstr(str);
cstr.Mid(0,5); // n =5 here,
LPCTSTR NewStr = cstr;


"Luigino" wrote:

> .
>

nexolite

unread,
Mar 5, 2010, 11:56:02 AM3/5/10
to
oh sorry made a typing mistake in the above reply it should be:

LPCTSTR str = _T("My Test String");
CString cstr(str);

LPCTSTR NewStr = cstr.Mid(0,5); // n =5 here

David Ching

unread,
Mar 5, 2010, 2:11:04 PM3/5/10
to
"Luigino" <npu...@rocketmail.com> wrote in message
news:a6584f98-f862-4fb9...@b7g2000yqd.googlegroups.com...

Is this a homework assignment?

-- David

Joseph M. Newcomer

unread,
Mar 5, 2010, 3:20:04 PM3/5/10
to
Your fundamental error is in thinking LPCTSTR is a sensible data type. You should be
using CString.

CString s;
s = ...;

CString truncated_s(s.Left(n));
CString rest_of_s(s.Mid(n + 1));

In MFC, it RARELY makes sense to be using LPCTSTR for any data in your own program. It is
something to be used under rare and exotic circumstances. In the worst case, I might
write a couple LPTSTR declarations in a program, mostly to get writeable buffers for API
calls. But I would never consider LPCTSTR as a reasonable data structure for ordinary
work. Or LPTSTR, either.

If you are seriously using LPTSTR/LPCTSTR, you should reconsider how you are writing code.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages