Page 1 of 2

Crop commands to cut strings short

Posted: Wed Nov 05, 2003 5:23 am
by PB
I often have to crop the last char off a string, using this:

Code: Select all

a$=Left(a$,Len(a$)-1)
I know it's not much to type, but it is when used in conjunction with
other commands (LCase, etc). It'd be great to have something like
a$=RCrop(a$,1) (where "1" = number of chars to crop).

You could even go further and have both LCrop(string$,chars) so
we could stop having to do this:

Code: Select all

a$=Mid(a$,startpos,chars)
:)

Posted: Wed Nov 05, 2003 9:48 am
by Fred
Is there a strandard name in other language for this ? Sounds a nice idea.

Posted: Wed Nov 05, 2003 10:00 am
by PB
> Is there a strandard name in other language for this ?

I don't think so, I haven't even seen them in other languages.
But I think LCrop (for removing the first X chars) and RCrop
are nice names for them -- after all, we're cropping the strings.

BTW, the reason I'd like LCrop is for the reason given at this
post: viewtopic.php?t=8193

Posted: Wed Nov 05, 2003 10:02 am
by LarsG
maybe "Cut" or "Trim" are better words?

Posted: Wed Nov 05, 2003 10:07 am
by PB
> maybe "Cut" or "Trim" are better words?

Trim exists for removing spaces at the moment. I guess parameters
could be added but it would break existing code in a big way.

Posted: Wed Nov 05, 2003 10:11 am
by LarsG
Yeah, I was thinking of LTrim and Rtrim.. but I just noticed these allready exist.. :lol:

Maybe Cut would be ok then.. or Slice even?!?

Posted: Wed Nov 05, 2003 10:19 am
by PB
> Maybe Cut would be ok then.. or Slice even?!?

Hehehe, I like crop because crop means to cut something short.

From http://dictionary.reference.com/search?q=crop :

To cut (hair, for example) very short.
To clip (an animal's ears, for example).
To trim (a photograph or picture, for example).

Posted: Wed Nov 05, 2003 10:19 am
by Rings
LarsG wrote:maybe "Cut" or "Trim" are better words?
'Trim' is for removing leading/trailing spaces

RCut or LCut are named perfect for this

A.s="Rings"

debug RCut(A.s,1)
prints "Ring"


easy command to implement, just do it like this:

;Rcut is easy
Procedure RCut(instringpointer.l,Pos)
PokeB(Instringpointer+lstrlen_(Instringpointer)-Pos,0)
EndProcedure

A.s="Siggi Rings"
RCut(@A.s,1)
Debug A.s


A.s="Siggi Rings"
LCut_Position=5
;LCut only with asm
!MOV eax,dword[v_LCut_Position]
!add dword[v_A],eax
Debug A.s

another topic is the StringBasepointer, i don't know if it must be realigned after this....but thats you turn Fred ;)

Posted: Wed Nov 05, 2003 10:49 am
by Berikco
Yeah, il like this command also
Lcut an Rcut sounds okay for me
crop sounds like vegetables in the garden ;)

Posted: Wed Nov 05, 2003 7:00 pm
by GPI
Fred wrote:Is there a strandard name in other language for this ? Sounds a nice idea.
BTW: FindString() is called in other languages instr() ...

Posted: Wed Nov 05, 2003 7:07 pm
by freak
> BTW: FindString() is called in other languages instr() ...

Yes, but instr is a very bad name for that. Back in my QB days, i could
never remember that name. FindString() is much better to remember,
and also describes the purpose of the function better.

Timo

Posted: Wed Nov 05, 2003 8:42 pm
by GPI
>Yes, but instr is a very bad name for that.

Maybe, but you search IN a STRing :)

And maybe it is a good idea to put the Macros on top of the todo-list (and of course, macros should be added in the res), so a simple

MACRO InSTR(Parent$,Find$,Position)=FindString(Parent$,Find$,Position)

make PB more standard (and maybe easier for newcomers from other Basic-Languages).

Posted: Thu Nov 06, 2003 12:17 pm
by Fred
I fear this abuse with the implementation of Macro. If everyone build its compatibility layer, it will be impossible to share it to the community. Anyway, anyone can do as they want.

Posted: Thu Nov 06, 2003 1:13 pm
by Kale
I fear this abuse with the implementation of Macro.
Me too :? i can see code getting muddled and harder to understand!

Posted: Thu Nov 06, 2003 3:21 pm
by Rings
Kale wrote:
I fear this abuse with the implementation of Macro.
Me too :? i can see code getting muddled and harder to understand!
nop, totaly disagree

code become more handy reading.

and of course all macros must been include at the top or via Includefile.pbi