Here's some code to catch and locate substrings in a string, in this case a comma.
Code: Select all
Function FindAndReplace(instring As String,ReplaceThis As String,WithThis As String) As String
var lens1=Len(ReplaceThis),lens2=Len(WithThis)
If lens1=lens2 Then lens1=0
dim as string s=instring
Dim As Integer position=Instr(s,ReplaceThis)
While position>0
If lens1 Then
s=Left(s,position-1) & WithThis & Mid(s,position+Lens1)
Else
Mid(s,position) = WithThis
End If
position=Instr(position+Lens2,s,ReplaceThis)
Wend
Function=s
End Function
Function COUNTER(instring As String,PartString As String,c() as integer) As integer
redim c(0)
dim count as integer
var lens2=len(PartString)
dim as string s=instring
Dim As Integer position=Instr(s,PartString)
redim preserve c(1 to ubound(c)+1):c(ubound(c))=position
While position>0
count=count+1
position=Instr(position+Lens2,s,PartString)
redim preserve c(1 to ubound(c)+1)
c(ubound(c))=position
Wend
redim preserve c(1 to ubound(c)-1)
Function=count
End Function
'============== MAKE A STRING =====================
dim as string g
#macro make(s)
#define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
s=string(intrange(1000,3000)," ")
for n as integer=0 to len(s)-1
var num=IntRange(0,128)
if num=7 or num=10 then num=48 'dont want beeps or spaces
s[n]=num
next n
s=","+s 'just add a comma at beginning for a check
#endmacro
make(g) 'make the string
'==================================================
dim as double t1,t2
dim as string substring=","
redim as integer p()
print "Length of string ";len(g)
print
print "A substring = ";substring
print
print "Number of occurencies of "& substring & " = ";
t1=timer
print counter(g,substring,p())
t2=timer
print
print "Time taken to find them ";t2-t1
print "Positions of these in the string"
print
'show the positions
for n as integer=1 to ubound(p)
print n,p(n)
next n
print "Now replace the substring with (----->A COMMA WAS HERE<-----)"
print
dim as string substring2="(----->A COMMA WAS HERE<-----)"
t1=timer
dim as string g2=FindAndReplace(g,substring,substring2)
print
print "Length of new string ";len(g2)
print
print "Number of occurencies of (----->A COMMA WAS HERE<-----) = ";
print counter(g2,substring2,p())
t2=timer
print "Time taken to replace them ";t2-t1
print "Start positions of these in the string"
print
'show the positions
for n as integer=1 to ubound(p)
print n, p(n)
next n
print
print "press a key to check the replacements"
sleep
print g2
sleep
press space to refresh, esc to end.
Code: Select all
#define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
Function FindAndReplace(instring As String,ReplaceThis As String,WithThis As String,flag As Integer=0) As String
If flag=1 Then Swap replacethis,withthis
Var lens1=Len(ReplaceThis),lens2=Len(WithThis)
If lens1=lens2 Then lens1=0
Dim As String s=instring
Dim As Integer position=Instr(s,ReplaceThis)
While position>0
If lens1 Then
s=Left(s,position-1) & WithThis & Mid(s,position+Lens1)
Else
Mid(s,position) = WithThis
End If
position=Instr(position+Lens2,s,ReplaceThis)
Wend
Function=s
End Function
Dim As String s1
#macro make()
s1=""
randomize timer
For z As Integer=1 To 1500
s1=s1+Chr(IntRange(48,49))
Next z
#endmacro
Print
Dim As String s2
Dim As Integer flag=0,length
screen 20
do
start:
if flag=0 then
make()
print "original"
print s1
end if
print
s2=FindAndReplace(s1,"000","a",flag)
s2=FindAndReplace(s2,"001","b",flag)
s2=FindAndReplace(s2,"010","c",flag)
s2=FindAndReplace(s2,"011","d",flag)
s2=FindAndReplace(s2,"100","e",flag)
s2=FindAndReplace(s2,"101","f",flag)
s2=FindAndReplace(s2,"110","g",flag)
s2=FindAndReplace(s2,"111","h",flag)
s2=FindAndReplace(s2,"00","j",flag)
s2=FindAndReplace(s2,"01","k",flag)
s2=FindAndReplace(s2,"10","l",flag)
s2=FindAndReplace(s2,"11","m",flag)
s2=FindAndReplace(s2,"aa","n",flag)
s2=FindAndReplace(s2,"bb","o",flag)
s2=FindAndReplace(s2,"cc","p",flag)
s2=FindAndReplace(s2,"dd","q",flag)
s2=FindAndReplace(s2,"ee","r",flag)
s2=FindAndReplace(s2,"ff","s",flag)
s2=FindAndReplace(s2,"gg","t",flag)
s2=FindAndReplace(s2,"hh","u",flag)
s2=FindAndReplace(s2,"jj","v",flag)
s2=FindAndReplace(s2,"kk","w",flag)
s2=FindAndReplace(s2,"ll","x",flag)
s2=FindAndReplace(s2,"mm","y",flag)
If flag=0 Then
Print "compress"
Print s2
Print
length=len(s2)
Print "length ";Length
Print
'REVERSE THE PROCESS:
flag=1:goto start
Else
Print "Original again"
Print s2
Print
Print "length ",Len(s2)
print "compressed length ",length
print "Ratio ",length/len(s2)
If s2=s1 Then Print "ok"
print
print "___________________________________________"
sleep
cls
goto _loop
End If
_loop:
s2="":flag=0
if inkey=chr(27) then end
loop