TGC Codebase Backup



12 Additional Text functions by Coin

23rd Feb 2005 17:38
Summary

Instr(), Cut$(), Insert$(), Replace$(), Count(), ReplaceAll$(), Itemstr$(), String$(), RightAdjust$(), Flip$(), Alpha$(), Match$(). I have migrated from a different Basic language



Description

The code with its functions will run and output examples to the screen in what I might describe as an adequate fashion. The first few lines beginning with the Print command, however, are merely examples of what the programme can produce and may be deleted. Rem statements are a bit sparse but sufficient to describe what parameters the functions require. Feel free to adapt the code to your own requirements. One minor warning, though: I designed them for my own use and therefore did not build in safeguards against spurious parameters, eg negative values. I hope it proves useful.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Example1$="abcdefaaabcdefababcdefa"
Example2$="one|two|three|four|five|six|seven|eight|nine|ten"
print "Example1$ = ";Example1$
print "Example2$ = ";Example2$
print
print "first 'fa' in Example1$ begins at position ";Instr("fa",Example1$,1)
print "john r doe with characters 6 & 7 cut out : ";Cut$("john r doe",6,7)
print "johndoe with 'richard' inserted : ";Insert$("johndoe"," richard ",5)
print "john richard doe with 'richard' replaced by 'r' : ";Replace$("john richard doe","richard","r")
print Example1$;" contains ";Count("a",Example1$);" a's"
print "all a's in Example1$ replaced by z : ";ReplaceAll$(Example1$,"a","z")
print "4th item of Example2$ is ";ItemStr$(4,Example2$,"|")
print "a string of 15 letter U's : ";String$("U",15)
print "24 right adjusted with zeroes & expanded to a length of 8 : ";RightAdjust$(24,8,0)
print "Example2$ flipped : ";Flip$(Example2$)
print "Example1$ arranged alphabetically : ";Alpha$(Example1$)
print "does 'a|c|e|f' match 'abcdefg'? 1=yes 0=no : ";Match("a|c|e|f","abcdefg","|")
print "does 'a|c|e|g' match 'abcdefg'? 1=yes 0=no : ";Match("a|c|e|g","abcdefg","|")

wait key
end

rem Delete this rem statement and everything above it

function Instr(Find$,SearchIn$,StartPoint)
  rem returns Result as the position of Find$ within SearchIn$; StartPoint is where search begins (defaults to 1)
  rem if Find$ is not found Result returns as 0
  if StartPoint=0 then StartPoint=1
  Result=0
  for Pointer=StartPoint to len(SearchIn$)-len(Find$)+1 step 1
    if mid$(SearchIn$,Pointer)=mid$(Find$,1)
      Position1=Pointer
      Position2=0
      while mid$(SearchIn$,Position1)=mid$(Find$,Position2+1) and Position2<len(Find$)
        inc Position1
        inc Position2
      endwhile
    endif
    if Position2=len(Find$)
      Result=Pointer
      Pointer=len(SearchIn$) : rem found it - stop looking
    endif
  next Pointer
endfunction Result

function Cut$(Original$,Position1,Position2)
  rem returns Result$=Original$ with characters Position1 through to Position2 removed
  Length=len(Original$)-Position2
  Result$=left$(Original$,Position1-1)+right$(Original$,Length)
endfunction Result$

function Insert$(Original$,Insert$,Position)
  rem returns Result$=Original$ with Insert$ inserted before character Position
  Length=len(Original$)-Position+1
  Result$=left$(Original$,Position-1)+Insert$+right$(Original$,Length)
endfunction Result$

function Replace$(Original$,Remove$,ReplaceWith$)
  rem returns Result$=Original$ with Remove$ Portion Replaced by ReplaceWith$
  rem if ReplaceWith$="" the function acts like the Cut$ function
  Length=len(Remove$)
  Pointer=Instr(Remove$,Original$,1)
  Result$=Cut$(Original$,Pointer,Pointer+Length-1)
  Result$=Insert$(Result$,ReplaceWith$,Pointer)
endfunction Result$

function Count(Find$,SearchIn$)
  rem returns Result as the number of times Find$ occurs in SearchIn$
  Result=0
  Pointer=Instr(Find$,SearchIn$,1)
  while Pointer>0
    SearchIn$=Cut$(SearchIn$,Pointer,Pointer)
    inc Result
    Pointer=Instr(Find$,SearchIn$,1)
  endwhile
endfunction Result

function ReplaceAll$(Original$,Remove$,ReplaceWith$)
  rem returns Result$=Original$ with all instances of Remove$ replaced by ReplaceWith$
  rem if ReplaceWith$="" all instances of Remove$ are deleted
  Result$=Original$
  Pointer=Instr(Remove$,Result$,1)
  while Pointer>0
    Result$=Replace$(Result$,Remove$,ReplaceWith$)
    Pointer=Instr(Remove$,Result$,1)
  endwhile
endfunction Result$

function ItemStr$(Number,SearchIn$,Delimiter$)
  rem returns Result$ as the Number element of SearchIn$ using Delimiter$ to separate the elements (I suggest "|")
  if Number<1 then Number=1
  if left$(SearchIn$,1)<>Delimiter$ then SearchIn$=Delimiter$+SearchIn$
  Counter=0
  Result$=""
  for Pointer=1 to len(SearchIn$)
    if mid$(SearchIn$,Pointer)=Delimiter$
      inc Counter
      if Counter=Number
        Result$=""
        Test$=mid$(SearchIn$,Pointer+1)
        if Test$<>Delimiter$
          Position=Pointer+1
          while Test$<>Delimiter$ and Position<=len(SearchIn$)
            Result$=Result$+Test$
            inc Position
            Test$=mid$(SearchIn$,Position)
          endwhile
        endif
      endif
    endif
  next Pointer
endfunction Result$

function String$(Character$,Number)
  rem returns Result$ as a string of Character$ Number in length
  Result$=""
  for Counter=1 to Number
    Result$=Result$+Character$
  next Counter
endfunction Result$

function RightAdjust$(Variable,Number,Mode)
  rem returns Result$ as (integer) Variable in string form of Number length
  rem Mode=0 padded with zeroes; Mode=1 padded with spaces
  Result$=str$(Variable)
  Length=len(Result$)
  if Number>Length
    Padding$="0"
    if Mode=1 then Padding$=" "
    Result$=String$(Padding$,Number-Length)+Result$
  endif
endfunction Result$

function Flip$(Original$)
  rem returns Result$ as Original$ reversed
  Result$=""
  for Pointer=len(Original$) to 1 step -1
    Result$=Result$+mid$(Original$,Pointer)
  next Pointer
endfunction Result$

function Alpha$(Original$)
  rem returns Result$ as Original$ arranged in alphabetical order
  Result$=""
  while Original$>""
    LowestCharacter=256
    for Pointer=1 to len(Original$)
      Ascii=asc(mid$(Original$,Pointer))
      if Ascii<LowestCharacter
        LowestCharacter=Ascii
        PointerToLowestChar=Pointer
      endif
    next Pointer
    Result$=Result$+chr$(LowestCharacter)
    Original$=Cut$(Original$,PointerToLowestChar,PointerToLowestChar)
  endwhile
endfunction Result$

function Match(Test$,TestAgainst$,WildCard$)
  rem Result=1 if Test$ matches TestAgainst$
  rem Result=0 if it does not match; WildCard$ is the wildcard in Test$
  Result=0
  if len(Test$)<>len(TestAgainst$) then exitfunction
  for Pointer=1 to len(Test$)
    if mid$(Test$,Pointer)<>mid$(TestAgainst$,Pointer) and mid$(Test$,Pointer)<>WildCard$ then Pointer=1000
  next Pointer
  if Pointer=len(Test$)+1 then Result=1
endfunction Result