_TITLE "Tools for Variable Length Array-Like Strings" '2021-02-19 all tests work with comma space delimiter, try comma alone next, OK! how about abc OK
'2021-02-20 renaming done all tests look OK, go over tomorrow before add to SB1.
' Todo change names of these functions
deli$ = ", ":
test$(1) = "1, 2, 3, 4"
test$(2) = "1, 2, 3"
test$(3) = "1, 2"
test$(4) = "1"
test$(5) = ""
'deli$ = ",":
'test$(1) = "1,2,3,4"
'test$(2) = "1,2,3"
'test$(3) = "1,2"
'test$(4) = "1"
'test$(5) = ""
'deli$ = "abc":
'test$(1) = "1abc2abc3abc4"
'test$(2) = "1abc2abc3"
'test$(3) = "1abc2"
'test$(4) = "1"
'test$(5) = ""
ins$ = "insert"
INPUT "Test GetVS$ function, give us an index to look up, 0 quits "; getUser
PRINT "*"; GetVS$
(deli$
, getUser
, test$
(1));
"*" '----------------------------------------- Test Delimiter/Word counter
PRINT "test StrCount&, counting *" + deli$
+ "*" PRINT test$
(i&
), "# of "; deli$; StrCount&
(deli$
, test$
(i&
)) ' ---------------------------------------- Test Delimiter/Word locator
PRINT "testing: "; test$
(1) PRINT "Find: " + TS$
(i&
) + "th delimiter is at "; StrPlace&
(deli$
, i&
, test$
(1)) ' ---------------------------------------- Test Insert between Multi Char Delimiters
PRINT:
PRINT:
INPUT "Press enter for some Insert tests... enter "; insertplace
PRINT "Insert from right to left in this string: "; test$
(testN
) PRINT:
PRINT "Test Insert, test string is *" + test$
(testN
) + "* insert at place #" + TS$
(insertplace
) result$ = AStringInsert$(deli$, ins$, insertplace, test$(testN))
PRINT:
INPUT "Press enter to continue... "; insertplace
'reuse variable instead of dim another ' ------------------------------------------ Test Replace String between Multi-Char Delimiters
PRINT:
PRINT:
INPUT "Press enter for some Replace tests... enter "; insertplace
PRINT "Replace from right to left in this string: " + test$
(testN
) PRINT:
PRINT "Test Replace, test string is *" + test$
(testN
) + "* Replace item #" + TS$
(insertplace
) result$ = SetVS$(deli$, ins$, insertplace, test$(testN))
PRINT:
INPUT "Press enter to continue... "; insertplace
'reuse variable instead of dim another PRINT "end of tests, goodbye! "
FUNCTION AStringInsert$
(Delimiter$
, Insert$
, NthPlace&
, AStringCopy$
) 'use: Function StrCount& (Char$, AString$)
'use: Function StrPlace& (Char$, Nth&, Astring$)
'use: FUNCTION StrCopies$ (NumberOfCopies&, S$)
REDIM Astring$
, wCnt&
, nthPlaceAt&
, head$
, tail$
Astring$ = AStringCopy$ 'AstringCopy$ gets changed so return result through function name$
wCnt& = StrCount&(Delimiter$, Astring$) + 1
'make sure we have enough delimiters
IF wCnt&
<= NthPlace&
THEN Astring$
= Astring$
+ StrCopies$
(NthPlace&
- wCnt&
, Delimiter$
) ' string$ is the problem!!!!! IF NthPlace&
<= 1 THEN 'If something there then it comes before but if nothing probably just starting out. IF Astring$
<> "" THEN Astring$
= Insert$
+ Delimiter$
+ Astring$
ELSE Astring$
= Insert$
ELSEIF NthPlace&
> wCnt&
THEN ' AString$ will be modified such that only insert has to be tacked to end after delimiter Astring$ = Astring$ + Insert$
nthPlaceAt& = StrPlace&(Delimiter$, NthPlace& - 1, Astring$)
head$
= MID$(Astring$
, 1, nthPlaceAt&
+ LEN(Delimiter$
) - 1) 'include delim tail$
= MID$(Astring$
, nthPlaceAt&
+ LEN(Delimiter$
)) 'no delim Astring$ = head$ + Insert$ + Delimiter$ + tail$
AStringInsert$ = Astring$
FUNCTION SetVS$
(Delimiter$
, Insert$
, NthPlace&
, AStringCopy$
) ' VS = Variable Siring Lengths 'use: FUNCTION StrCount& (S$, AString$)
'use: FUNCTION StrPlace& (S$, Index AS LONG, Astring$)
'use: FUNCTION StrCopies$ (NumberOfCopies&, S$)
REDIM Astring$
, wCnt&
, nthPlaceAt&
, nextAt&
Astring$ = AStringCopy$ 'AstringCopy$ gets changed so return result through function name$
wCnt& = StrCount&(Delimiter$, Astring$) + 1
'make sure we have enough delimiters
IF wCnt&
<= NthPlace&
THEN Astring$
= Astring$
+ StrCopies$
(NthPlace&
- wCnt&
, Delimiter$
) ' string$ is the problem!!!!! IF NthPlace&
> wCnt&
THEN ' AString$ will be modified such that only insert has to be tacked to end after delimiter Astring$ = Astring$ + Insert$
ELSEIF wCnt&
= 1 THEN 'If something there then it comes before but if nothing probably just starting out. Astring$ = Insert$
ELSE ' NthPlace& is between 2 delimiters nthPlaceAt& = StrPlace&(Delimiter$, NthPlace& - 1, Astring$)
nextAt& = StrPlace&(Delimiter$, NthPlace&, Astring$)
IF NthPlace&
= wCnt&
THEN 'no delim on right end Astring$
= MID$(Astring$
, 1, nthPlaceAt&
+ LEN(Delimiter$
) - 1) + Insert$
IF nextAt&
THEN Astring$
= Insert$
+ MID$(Astring$
, nextAt&
) ELSE Astring$
= Insert$
ELSE 'between 2 delimiters Astring$
= MID$(Astring$
, 1, nthPlaceAt&
+ LEN(Delimiter$
) - 1) + Insert$
+ MID$(Astring$
, nextAt&
) SetVS$ = Astring$
FUNCTION GetVS$
(Delimiter$
, Index
AS LONG, AString$
) ' VS for Variable length string, 'use: FUNCTION StrCount& (S$, AString$)
'use: FUNCTION StrPlace& (S$, Index AS LONG, Astring$)
cnt = StrCount&(Delimiter$, AString$) + 1
p1 = StrPlace&(Delimiter$, Index - 1, AString$)
p2 = StrPlace&(Delimiter$, Index, AString$)
GetVS$
= MID$(AString$
, 1, p2
- 1) GetVS$
= MID$(AString$
, p1
+ LEN(Delimiter$
)) GetVS$
= MID$(AString$
, p1
+ LEN(Delimiter$
), p2
- p1
- LEN(Delimiter$
))
FUNCTION StrCopies$
(NumberOfCopies&
, S$
) ' Concatenate repeated copies of S$ FOR i&
= 1 TO NumberOfCopies&
StrCopies$ = StrCopies$ + S$
FUNCTION StrCount&
(S$
, AString$
) ' Count S$ in Astring$ place
= INSTR(AString$
, S$
): lenS
= LEN(S$
) cnt = cnt + 1
place
= INSTR(place
+ lenS
, AString$
, S$
) StrCount& = cnt
FUNCTION StrPlace&
(S$
, Index
AS LONG, Astring$
) ' Locate the Index number S$ in Astrin$ place
= INSTR(Astring$
, S$
): lenS
= LEN(S$
) cnt = cnt + 1
place
= INSTR(place
+ lenS
, Astring$
, S$
)