Pelles C forum

C language => Pocket PC and Smartphone questions => Topic started by: gromit on May 12, 2012, 04:44:14 pm

Title: delete end of string?
Post by: gromit on May 12, 2012, 04:44:14 pm
Hi all having a wee bit of difficulty here
Could someone pls help;

Following code finds the word subtotal in the wide string fstr
wchar_t fstr[2000];
int len=(GetWindowTextLength(g_heditqoutebox));
GetDlgItemText(g_hwnd,QUOTE_EDITBOX,fstr,len+1);

wchar_t *ptrx=wcsstr(fstr, L"subtotal");// find where in the string we find subtotal
if (ptrx!=NULL) // found subtotal
{   
// now need to delete everything beyond and including subtotal


   SetFocus(g_heditqoutebox);
}





 




 
Whilst i have the pointer to the beginning of "subtotal" i wish to calculate the length of the wide string up to the beginning of that word and delete from the wide string all text from subtotal to the end of the string

e.g the "massive subtotal is big" needs to become just
"massive"

BTIA gromit
Title: Re: delete end of string?
Post by: Stefan Pendl on May 12, 2012, 05:44:18 pm
The easiest is to set the pointer of subtotal to \0 or '0', which is the null character.

You can copy the string afterwards and replace '0' by the 's' to get the initial string back too.
Title: Re: delete end of string?
Post by: CommonTater on May 12, 2012, 05:54:57 pm
Like Stephan says...

Code: [Select]
if (ptrx)
  *ptrx = 0;

length = wcslen(fstr);

Easy stuff...
Title: Re: delete end of string?
Post by: gromit on May 12, 2012, 06:42:07 pm
Thanks Tater
Thanks Stefan Pendle

I do enjoy programming but as i get older i seem to easily overlook the obvious

now which one of these emoticons shows aged embarassment :-\ :-\ :-\ :-\ :-\
Title: Re: delete end of string?
Post by: Stefan Pendl on May 12, 2012, 07:22:50 pm
I do enjoy programming but as i get older i seem to easily overlook the obvious
Already been there at a not so advanced age ::)

Thinking keeps your brain fit for life ;)
Title: Re: delete end of string?
Post by: CommonTater on May 12, 2012, 09:44:44 pm
Thanks Tater
Thanks Stefan Pendle

I do enjoy programming but as i get older i seem to easily overlook the obvious

now which one of these emoticons shows aged embarassment :-\ :-\ :-\ :-\ :-\

One thing to keep in mind is that if you are looking at a word, it is likely that setting a null to kill the remainder of the string may leave you with a trailing space... it's up to you to decide if that could cause problems with your code.

This might work a little better...
Code: [Select]
if (ptrx)
  { do
      { *ptrx = 0;
           ptrx--; }
  while (iswspace(*ptrx)); }

That would clean any trailing whitespace off the string.

And don't worry about the age thing... It lets you use the "Alzheimer Moment" excuse to get out of trouble.  (Or at least it works for me :D )

 
Title: Re: delete end of string?
Post by: gilroy on June 26, 2013, 01:08:15 pm
I program using a mixture of c and Win32 API ( i know they are one and the same thing in a way)
I know under windows you first have to have a default printer etc
but PDAs (psion being the exeption) dont generally have this facility built in
I would like to do something myself as there are not many third party progs about.