Gary
10-09-2023, 18:24
ReplaceAll - replaces all occurances of findstr with NewStr
replaceOne - replaces one occurance of findstr with NewStr
BetweenStr - returns string between two delimiters
ParseBetween - returns string between two delimiters by index
ParseBetweenReplace -returns string between two delimiters by index and replaces value
'---Script created on 09-10-2023 07:45:50 by Gary Robinson
Uses "console"
'testing oop to create a function module with between and replace functions
Type BetweenReplace
Private
mainStr as string
public
Function _Create(mainString as string) As Long
'---------------------------------------------------
me.mainStr=mainString
End Function
function printMainstr() as string
print me.mainStr + $crlf
end function
function toVariable() as string
function =me.mainStr
end function
function NewMainString(newMain as string)
me.mainStr = newMain
end function
function ReplaceAll(findstr AS STRING,replaceStr AS STRING) as string
'thinbasic replace function
me.mainStr = replace$(me.mainstr,findStr,replaceStr)
end function
FUNCTION replaceOne(findstr AS STRING,replaceStr AS STRING) AS STRING
'front match
DIM front AS STRING,back AS STRING
DIM frntPos AS LONG,bckPos AS LONG
IF INSTR(me.mainStr,findStr) THEN
frntPos=INSTR(me.mainStr,findstr)
bckPos=frntPos+LEN(findstr)
me.mainStr=MID$(me.mainStr,1,frntpos-1)+replaceStr+MID$(me.mainStr,bckPos)
ELSE
FUNCTION=me.mainStr
END IF
END FUNCTION
FUNCTION BetweenStr( delimiter as string, delimiter2 as string) as string 'find the text between two markers... this is a very useful function!
dim sText as string,posFirst as long,posSecond as long
posFirst = instr(me.mainStr,delimiter)-1
posFirst = posFirst + len(delimiter)+1
posSecond = instr(me.mainStr,delimiter2)
function=mid$(me.mainStr,posFirst,posSecond-posFirst)
END FUNCTION
Function ParseBetween(delimiter as string,delimiter2 as string,index as long) as string
dim first as string
dim p as long,q as long
p=instr(1,me.mainStr,delimiter,index)
q=instr(1,me.mainStr,delimiter2,index)
if (p=0 or q=0) Then
function = sMainString
else
first=parse$(me.mainStr,delimiter,index+1)
function=parse$(first,delimiter2,1)
end if
End Function
Function ParseBetweenReplace(delimiter as string,delimiter2 as string,indx as long,replaceStr as string) as string
dim sss as string,i as long,p as long,ddd as string,q as long
p=instr(1,me.mainStr,delimiter,indx)
q=instr(1,me.mainStr,delimiter2,indx)
if (p=0 or q=0) Then
function = me.mainStr
else
sss=mid$(me.mainStr,1,p+len(delimiter)-1)
ddd=mid$(me.mainStr,q)
me.mainStr=sss+replaceStr+ddd
end if
End Function
end type
dim main as string
dim tmp as string
main = "<TR><TD> John Doe </TD><TD> 123-45-6789 </TD></TR>" + $crlf
Dim br As BetweenReplace( main )
print "========================================================"+$crlf
print "===ParseBetweenReplace=================================="+$crlf
print br.ParseBetweenReplace("<TD>","</TD>",1," John Smith ") + $crlf
Print br.ParseBetweenReplace("<TD>","</TD>",2," 999-99-9999 ") + $crlf
print br.PrintMainStr
print $crlf
print "========================================================"+$crlf
print "===ParseBetween========================================="+$crlf
br.NewMainString(main)
Print br.ParseBetween("<TD>","</TD>",1) + $crlf
Print br.ParseBetween("<TD>","</TD>",2) + $crlf
print $crlf
print "========================================================"+$crlf
print "===BetweenStr========================================="+$crlf
Print br.BetweenStr("<TD>","</TD>") + $crlf
print $crlf
print "========================================================"+$crlf
print "===ReplaceAll==========================================="+$crlf
print br.ReplaceAll("<TD>","<>")
Print br.ReplaceAll("</TD>","</>")
print br.PrintMainStr
print $crlf
br.NewMainString(main)
print "========================================================"+$crlf
print "===ReplaceOne==========================================="+$crlf
print br.ReplaceOne("<TD>","<>") + $crlf
Print br.ReplaceOne("</TD>","</>")
print br.PrintMainStr
print "========================================================"+$crlf
print "===toVariable==========================================="+$crlf
tmp = br.toVariable()
print tmp +$crlf
Console_WaitKey
replaceOne - replaces one occurance of findstr with NewStr
BetweenStr - returns string between two delimiters
ParseBetween - returns string between two delimiters by index
ParseBetweenReplace -returns string between two delimiters by index and replaces value
'---Script created on 09-10-2023 07:45:50 by Gary Robinson
Uses "console"
'testing oop to create a function module with between and replace functions
Type BetweenReplace
Private
mainStr as string
public
Function _Create(mainString as string) As Long
'---------------------------------------------------
me.mainStr=mainString
End Function
function printMainstr() as string
print me.mainStr + $crlf
end function
function toVariable() as string
function =me.mainStr
end function
function NewMainString(newMain as string)
me.mainStr = newMain
end function
function ReplaceAll(findstr AS STRING,replaceStr AS STRING) as string
'thinbasic replace function
me.mainStr = replace$(me.mainstr,findStr,replaceStr)
end function
FUNCTION replaceOne(findstr AS STRING,replaceStr AS STRING) AS STRING
'front match
DIM front AS STRING,back AS STRING
DIM frntPos AS LONG,bckPos AS LONG
IF INSTR(me.mainStr,findStr) THEN
frntPos=INSTR(me.mainStr,findstr)
bckPos=frntPos+LEN(findstr)
me.mainStr=MID$(me.mainStr,1,frntpos-1)+replaceStr+MID$(me.mainStr,bckPos)
ELSE
FUNCTION=me.mainStr
END IF
END FUNCTION
FUNCTION BetweenStr( delimiter as string, delimiter2 as string) as string 'find the text between two markers... this is a very useful function!
dim sText as string,posFirst as long,posSecond as long
posFirst = instr(me.mainStr,delimiter)-1
posFirst = posFirst + len(delimiter)+1
posSecond = instr(me.mainStr,delimiter2)
function=mid$(me.mainStr,posFirst,posSecond-posFirst)
END FUNCTION
Function ParseBetween(delimiter as string,delimiter2 as string,index as long) as string
dim first as string
dim p as long,q as long
p=instr(1,me.mainStr,delimiter,index)
q=instr(1,me.mainStr,delimiter2,index)
if (p=0 or q=0) Then
function = sMainString
else
first=parse$(me.mainStr,delimiter,index+1)
function=parse$(first,delimiter2,1)
end if
End Function
Function ParseBetweenReplace(delimiter as string,delimiter2 as string,indx as long,replaceStr as string) as string
dim sss as string,i as long,p as long,ddd as string,q as long
p=instr(1,me.mainStr,delimiter,indx)
q=instr(1,me.mainStr,delimiter2,indx)
if (p=0 or q=0) Then
function = me.mainStr
else
sss=mid$(me.mainStr,1,p+len(delimiter)-1)
ddd=mid$(me.mainStr,q)
me.mainStr=sss+replaceStr+ddd
end if
End Function
end type
dim main as string
dim tmp as string
main = "<TR><TD> John Doe </TD><TD> 123-45-6789 </TD></TR>" + $crlf
Dim br As BetweenReplace( main )
print "========================================================"+$crlf
print "===ParseBetweenReplace=================================="+$crlf
print br.ParseBetweenReplace("<TD>","</TD>",1," John Smith ") + $crlf
Print br.ParseBetweenReplace("<TD>","</TD>",2," 999-99-9999 ") + $crlf
print br.PrintMainStr
print $crlf
print "========================================================"+$crlf
print "===ParseBetween========================================="+$crlf
br.NewMainString(main)
Print br.ParseBetween("<TD>","</TD>",1) + $crlf
Print br.ParseBetween("<TD>","</TD>",2) + $crlf
print $crlf
print "========================================================"+$crlf
print "===BetweenStr========================================="+$crlf
Print br.BetweenStr("<TD>","</TD>") + $crlf
print $crlf
print "========================================================"+$crlf
print "===ReplaceAll==========================================="+$crlf
print br.ReplaceAll("<TD>","<>")
Print br.ReplaceAll("</TD>","</>")
print br.PrintMainStr
print $crlf
br.NewMainString(main)
print "========================================================"+$crlf
print "===ReplaceOne==========================================="+$crlf
print br.ReplaceOne("<TD>","<>") + $crlf
Print br.ReplaceOne("</TD>","</>")
print br.PrintMainStr
print "========================================================"+$crlf
print "===toVariable==========================================="+$crlf
tmp = br.toVariable()
print tmp +$crlf
Console_WaitKey