Suggestion - RIGHT$ (and LEFT$)

Previous topic - Next topic

PeeJay

to join MID$ - these commands take the rightmost (or leftmost) characters from a string - very useful for leading zeroes - for example

PRINT "Score: "+RIGHT$("000000"+score,6)

would take the last 6 characters of the string, giving:

Score: 000007
Score: 000042
Score: 012345
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

Neurox

Hi PeeJay,
I've wrote this function for you
Code (glbasic) Select
// --------------------------------- //
// Project: TESTZERO$
// Start: Monday, January 14, 2008
// IDE Version: 5.129
score = 1966
PRINT zero$(score,10,TRUE),10,10
PRINT zero$(score,8,FALSE),10,20
SHOWSCREEN
KEYWAIT

// ------------------------------------------------------------- //
// ---  ZERO$  ---
// ------------------------------------------------------------- //
FUNCTION zero$: wnum, wlen, wlor //wlor= FALSE = Left - TRUE = Right
LOCAL wi,wnlen,wstr$,wzero$
wstr$ = wnum
wnlen = LEN(wstr$)
IF wnlen < wlen
   FOR wi = 0 TO (wlen - wnlen - 1)
      wzero$ = wzero$ + "0"
   NEXT
   IF wlor = TRUE
      RETURN wzero$+wstr$
   ELSE
      RETURN wstr$+wzero$
   ENDIF
ELSE
   RETURN wstr$
ENDIF

ENDFUNCTION // ZERO$
Bye bye,
Neurox
Paolo Borzini | paolo@borzini.it
The WhiteFly Software | www.thewhitefly.it
Service on line for screen printers | www.4pellicole.it

PeeJay

Thanks Neurox - that is great for this, but I still think they are very useful commands for string splicing, for word wrap and the like
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

bigsofty

Code (glbasic) Select
FUNCTION RIGHT$: str$, l
RETURN MID$(str$,LEN(str$) - l, l)
ENDFUNCTION

FUNCTION LEFT$: str$, l
RETURN MID$(str$,0,l)
ENDFUNCTION
P.S. thats a lowercase 'L' not a 1, in the function parameter.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

Instead of 'l' I usually type "lng"

flet

Could MID$ accept negative parameter (counting from end of string instead of begining)?

flet

another suggestion: NOT and XOR operators (on true/false values): eg. IF NOT myboolean THEN ...