Looking for the best "Parse"...
- Posted by Cassidy Napoli <gonzotek at yahoo.com> Oct 25, 2001
- 407 views
I want to be able to let the user specify a string of text with variables in it that my program will replace. The goal is to put the current winamp song into a line of text. I wrote this function, which works, but after reading the "Best Join" discussion, I realized there might be a better way of doing it. So I am looking for any faster or simpler methods, if there are any. I'll gladly accept any comments on my coding style, too. I want to improve it. [begin code] include wildcard.e global function parseUserText(sequence unParsedText, sequence escape, sequence replace ) sequence ParsedText integer varPos varPos = match(lower(escape), lower(unParsedText)) if not varPos then return unParsedText elsif varPos = (length(unParsedText) - length(escape) + 1 ) then ParsedText = unParsedText[1..(length(unParsedText) - length(escape))] & replace return parseUserText(ParsedText, escape, replace) else if varPos = 1 then ParsedText = replace & unParsedText[(length(escape) + 1)..length(unParsedText)] return parseUserText(ParsedText, escape, replace) else ParsedText = unParsedText[1..(varPos - 1)] & replace & unParsedText[(varPos + (length(escape)))..length(unParsedText)] return parseUserText(ParsedText, escape, replace) end if end if end function --ex. text = parseUserText("The $A jumped over the lazy dog.","$A","quick brown fox") puts (1, parseUserText("The $A jumped over the lazy dog.","$A","quick brown fox")) [end code]