LEFT() & RIGHT()

General FreeBASIC programming questions.
Post Reply
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

LEFT() & RIGHT()

Post by albert »

@coders

Could you guys make the left() & right() statements , so you can set values for them??

Like:

left( string , 5 ) = "Hello"
right( string , 3 ) = "End"

Like the mid() statement....
Julcar
Posts: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: LEFT() & RIGHT()

Post by Julcar »

To be honest, I always used Left and Right as functions, never as SUBs, in fact I have a Replace() function when I need to change some substrings

Code: Select all

FUNCTION Replace (Query AS STRING, LookFor AS STRING, ReplaceWith AS STRING) AS STRING
  DIM AS INTEGER FoundPos, StartPos, NextPos
  DIM AS STRING LeftStr, RightStr, Result
  Result = Query
  DO
    FoundPos = INSTR(Result, LookFor)
    IF FoundPos <> 0 THEN
      StartPos = FoundPos - 1
      NextPos = FoundPos + LEN(LookFor)
      LeftStr = LEFT(Result, StartPos)
      RightStr = MID(Result, NextPos, LEN(Result))
      Result = LeftStr + ReplaceWith + RightStr
    ELSE
      EXIT DO
    END IF
  LOOP
  Replace = Result
  'Clean memory
  Result = ""
END FUNCTION
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: LEFT() & RIGHT()

Post by jj2007 »

Code: Select all

Dim As String test="What a wonderful world"
Mid(test, 1, 2)="Th"
print test
Mid(test, Len(test)-4, 4)="word "
print test

sleep
Post Reply