/******************************************************************* Title: STRING FUNCTIONS Author: Scraggle - aka Craig Bryan Published - 17th May 2015 ******************************************************************** Functions: MyReplaceString() replaces one or more occurences of a tring with another if found inside the 'source' string insertString() inserts a string inside another at the requested position rightFrom() returns a string that is the right of the source string except for the number of characters specified by 'pos' extractString() returns whatever appears in the source string between the two strings passed to it removeString() returns the 'source' string without the 'remove' part in it instr() returns the position of a string inside another countString() returns the number of times one string appears within another quote() returns a string inside double quotes padLeft() inserts the left char of 'pad' to the left of 'source' n number of time until the returned string is 'size' long padRight() inserts the left char of 'pad' to the right of 'source' n number of time until the returned string is 'size' long CR$ - inserts a Carriage Return CR$ - inserts a Line Feed CRLF$ - inserts a Carriage Return and a line feed TAB$ - inserts a TAB QUOTE$ - inserts souble quotes */ #constant CR$ chr(13) #constant LF$ chr(10) #constant CRLF$ chr(13)+chr(10) #constant TAB$ chr(9) #constant QUOTE$ chr(34) /////////////////////// //To Upper function ToUpper( source as string ) r$="" for k = 0 to len(source) this = asc(mid(source, k, 1)) if (this>96) and (this<123) r$=r$+chr(this-32) else r$=r$+chr(this) endif next k endfunction r$ /////////////////////// //To Lower function ToLower( source as string ) r$="" for k = 0 to len(source) this = asc(mid(source, k, 1)) if (this>64) and (this<91) r$=r$+chr(this+32) else r$=r$+chr(this) endif next k endfunction r$ //////////////////////// //PAD LEFT function padLeft( source as string, pad as string, maxSize as integer ) r$ = source size = len(source) if size>=maxSize then exitfunction source pad$ = left(pad, 1) for k = 1 to maxSize-size insert$ = insert$ + pad$ next k r$ = insertString( source, insert$, 0 ) endfunction r$ function padLeftI( source as integer, pad as string, maxSize as integer ) r$ = str(source) size = len(r$) if size>=maxSize then exitfunction r$ pad$ = left(pad, 1) for k = 1 to maxSize-size insert$ = insert$ + pad$ next k r$ = insertString( str(source), insert$, 0 ) endfunction r$ function padLeftF( source as float, pad as string, maxSize as integer ) r$ = str(source) size = len(r$) if size>=maxSize then exitfunction r$ pad$ = left(pad, 1) for k = 1 to maxSize-size insert$ = insert$ + pad$ next k r$ = insertString( str(source), insert$, 0 ) endfunction r$ //////////////////////// //PAD RIGHT function padRight( source as string, pad as string, maxSize as integer ) r$ = source size = len(source) if size>=maxSize then exitfunction source pad$ = left(pad, 1) for k = 1 to maxSize-size insert$ = insert$ + pad$ next k r$ = insertString( source, insert$, len(source) ) endfunction r$ function padRightI( source as integer, pad as string, maxSize as integer ) r$ = str(source) size = len(r$) if size>=maxSize then exitfunction r$ pad$ = left(pad, 1) for k = 1 to maxSize-size insert$ = insert$ + pad$ next k r$ = insertString( str(source), insert$, len(str(source)) ) endfunction r$ function padRightF( source as float, pad as string, maxSize as integer ) r$ = str(source) size = len(r$) if size>=maxSize then exitfunction r$ pad$ = left(pad, 1) for k = 1 to maxSize-size insert$ = insert$ + pad$ next k r$ = insertString( str(source), insert$, len(str(source)) ) endfunction r$ //////////////////////// //QUOTE STRING function quote( source$ ) q$ = QUOTE$+source$+QUOTE$ endfunction q$ //////////////////////// //REPLACE STRING function MyReplaceString( source as string, removeThis as string, insertThis as string, qty as integer ) r$=source if qty = 0 then exitfunction source found = countString(source, removeThis, 0) if found = 0 then exitfunction source if qty > found then qty = found if qty = -1 then qty = found lastStart = 0 for k = 1 to qty startPos = instr(r$, removeThis, lastStart)-1 r$ = removeString(r$, removeThis) r$ = insertString(r$, insertThis, startPos ) next k endfunction r$ //////////////////////// //INSERT STRING function insertString( source as string, insert as string, pos as integer ) r$ = source if pos < 0 then pos = 0 left$ = left(source, pos ) right$ = rightFrom( source, pos) r$ = left$+insert+right$ endfunction r$ //////////////////////// //RIGHT FROM function rightFrom( source as string, pos as integer ) r$ = mid(source, pos+1, -1) endfunction r$ function rightFrom2( source as string, delim as string ) pos=instr(source,":", 0) r$ = mid(source, pos+1, -1) endfunction r$ //////////////////////// //EXTRACT STRING function extractString( source as string, left$ as string, right$ as string, start as integer ) r$ = source if left$="" leftPos = 0 else leftPos = instr(source, left$, start) + 1 endif if right$="" rightPos = len(source)+1 else rightPos = instr(source, right$, leftPos+1) endif r$ = mid(source, leftPos, rightPos-leftPos) endfunction r$ //////////////////////// //REMOVE STRING function removeString( source as string, remove as string ) r$=source if instr(source, remove, 0) = -1 exitfunction source endif start = instr(source, remove, 0) if start>-1 left$=left(source, start-1) else left$="" endif right$ = mid( source, start+len(remove), -1) r$ = left$+right$ endfunction r$ /////////////////////////// // INSTR function instr(source as string, search as string, start as integer ) if (start > len(source)-len(search)+1) or (start < 0) exitfunction -1 endif for k = start to len(source)-len(search)+1 if mid(source, k, len(search)) = search exitfunction k endif next k endfunction -1 ////////////////////////// // COUNT function countString( source as string, search as string, start as integer ) if (start > len(source)-len(search)+1) or (start < 0) exitfunction 0 endif count = 0 for k = 0 to len(source) if mid(source, k, len(search)) = search inc count endif next k endfunction count