Hi,
There are quite a few string functions which would be very handy if AppGameKit included them as standard but since it doesn't (at time of writing) here are a few to play with:
replaceString()
replaces one or more occurrences of a string 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 a number of characters to the left of the source string.
Handy for displaying a score for example:
456 becomes 00456
padRight()
Much like padLeft but to the right instead
/*******************************************************************
Title: STRING FUNCTIONS
Author: Scraggle - aka Craig Bryan
Published - 17th May 2015
********************************************************************
Functions:
replaceString()
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 a number of characters to the left of the source string.
Handy for displaying a score for example:
456 becomes 00456
padRight()
Much like padLeft but to the right instead
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)
////////////////////////
//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$
////////////////////////
//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$
////////////////////////
//QUOTE STRING
function quote( source$ )
q$ = QUOTE$+source$+QUOTE$
endfunction q$
////////////////////////
//REPLACE STRING
function replaceString( source as string, removeThis as string, insertThis as string, qty as integer )
r$=source
if qty < 1 then exitfunction source
found = countString(source, removeThis, 0)
if found = 0 then exitfunction source
if qty > found 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$ = rightString( source, pos)
r$ = left$+insert+right$
endfunction r$
////////////////////////
//RIGHT FROM
function rightString( source as string, pos as integer )
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
leftPos = instr(source, left$, start) + 1
rightPos = instr(source, right$, leftPos+1)
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
AGK V2 user - Tier 1 (mostly)