NO

Author Topic: delete end of string?  (Read 12378 times)

gromit

  • Guest
delete end of string?
« 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

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: delete end of string?
« Reply #1 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.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: delete end of string?
« Reply #2 on: May 12, 2012, 05:54:57 PM »
Like Stephan says...

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

length = wcslen(fstr);

Easy stuff...

gromit

  • Guest
Re: delete end of string?
« Reply #3 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 :-\ :-\ :-\ :-\ :-\

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: delete end of string?
« Reply #4 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 ;)
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: delete end of string?
« Reply #5 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 )

 
« Last Edit: May 12, 2012, 09:49:12 PM by CommonTater »

gilroy

  • Guest
Re: delete end of string?
« Reply #6 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.