GLBasic forum

Main forum => GLBasic - en => Topic started by: PeeJay on 2008-Jan-14

Title: Suggestion - RIGHT$ (and LEFT$)
Post by: PeeJay on 2008-Jan-14
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
Title: Suggestion - RIGHT$ (and LEFT$)
Post by: Neurox on 2008-Jan-14
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
Title: Suggestion - RIGHT$ (and LEFT$)
Post by: PeeJay on 2008-Jan-14
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
Title: Suggestion - RIGHT$ (and LEFT$)
Post by: bigsofty on 2008-Jan-14
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.
Title: Suggestion - RIGHT$ (and LEFT$)
Post by: Kitty Hello on 2008-Jan-15
Instead of 'l' I usually type "lng"
Title: Suggestion - RIGHT$ (and LEFT$)
Post by: flet on 2008-Jan-15
Could MID$ accept negative parameter (counting from end of string instead of begining)?
Title: Suggestion - RIGHT$ (and LEFT$)
Post by: flet on 2008-Jan-22
another suggestion: NOT and XOR operators (on true/false values): eg. IF NOT myboolean THEN ...