_TITLE "Word tools Demo" 'from: Word tools 2018-03-23.bas" 'B+ 2019-04-15
test$ = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 "
PRINT "Test string: *"; test$;
"*" test$ = wPrep$(test$)
PRINT:
PRINT "Test string run through wPrep$() to remove excess spaces:" PRINT:
PRINT "Test string has"; wCnt
(test$
);
"'words' in it." PRINT:
PRINT "Show every third word in test string:" PRINT:
PRINT "What multiples of 5 are in test string?" PRINT:
PRINT "Substitute the phrase 'and all the rest...' for words after 10:" PRINT wSubst$
(test$
, 11, wCnt
(test$
), "and all the rest...")
'return trimmed source string s with one space between each word
'remove all double or more spaces
b$ = ""
wPrep$ = b$
' This duplicates JB word(string, wordNumber) base 1, space as default delimiter
' by returning the Nth word of source string s
' this function assumes s has been through wPrep
's$ = wPrep(ss$)
s$ = ss$ 'don't change ss$
w$ = "": c = 1
w$ = "": c = c + 1
'This function counts the words in source string s
'this function assumes s has been thru wPrep
's = wPrep(s)
c
= 1: p
= 1: ip
= INSTR(p
, s$
, " ") c
= c
+ 1: p
= ip
+ 1: ip
= INSTR(p
, s$
, " ") wCnt = c
'Where is word In source s, 0 = Not In source
'this function assumes s has been thru wPrep
wc = wCnt(s$): wIn = 0
' substitute string in s to replace section first to last words inclusive
'this function assumes s has been thru wPrep
FUNCTION wSubst$
(s$
, first
, last
, subst$
) wc = wCnt(s$): b$ = ""
IF first
<= i
AND i
<= last
THEN 'do this only once! IF subF
= 0 THEN b$
= b$
+ subst$
+ " ": subF
= 1 b$ = b$ + Wrd$(s$, i) + " "