IonicWind Software

IWBasic => General Questions => Topic started by: Rock Ridge Farm (Larry) on December 04, 2008, 08:05:52 AM

Title: String functions
Post by: Rock Ridge Farm (Larry) on December 04, 2008, 08:05:52 AM
Does ebasic have string functions similar to C string functions?
I need to do the following:
stringa = "this is a string containing %stuff% and more %things% and so on"
I want to create a second string that substitutes variable length text in place of the %...%.
examples of the inserts could be:
stuff = "rocks, boxes, trees"
things = "this is a bunch of things"

Is there an easy way to do this?
Title: Re: String functions
Post by: Hootie on December 04, 2008, 08:16:42 AM
stringa = USING("&&&&&","this is a string containing ", stuff, " and more ", things, " and so on")

This is one way to do it where stuff and things are string variables containing anything.
Title: Re: String functions
Post by: Ionic Wind Support Team on December 04, 2008, 08:22:33 AM
Or even:

stringa = USING("this is a string containing & and more &", stuff,  things)

Which is a little more readable.

Paul.
Title: Re: String functions
Post by: Rock Ridge Farm (Larry) on December 04, 2008, 08:50:42 AM
I almost understand - but ---
string a is passed to me from another function.
I do not know where or what will be between the %% (keyword)
I have a list of keywords and depending on what is between the %% will
determine the substitution.

So - how would I do this with USING?
Title: Re: String functions
Post by: billhsln on December 04, 2008, 09:37:14 AM
The  following code should do what you are looking for:

' PCT = String to find
DEF PCT:STRING
' SSTR = Starting String
DEF SSTR:STRING
' NSTR = New String
DEF NSTR:STRING
' RSTR = Replacement for PCT
DEF RSTR:STRING

DEF I,L:INT

OPENCONSOLE

PCT = "%TEST%"
SSTR = "Replace %TEST% with something else"
RSTR = "testing"
L = LEN(PCT)
I = INSTR(SSTR,PCT)
IF I > 0 THEN NSTR = MID$(SSTR,1,I - 1) + RSTR + MID$(SSTR, I + L)
PRINT NSTR

DO
UNTIL INKEY$ <> ""

CLOSECONSOLE
END


Hope this helps,
Bill
Title: Re: String functions
Post by: sapero on December 04, 2008, 09:47:26 AM
I think the ExpandEnvironmentStrings function will be faster
$include "windowssdk.inc"

SetEnvironmentVariable("stuff", "stuff_value")
SetEnvironmentVariable("things", "things_value")

string out
ExpandEnvironmentStrings("Hello %username%, this is a string containing %stuff% and more %things% and so on", out, 256)
print out
_system("pause")
Title: Re: String functions
Post by: Rock Ridge Farm (Larry) on December 04, 2008, 12:01:58 PM
I do not seem to have windowssdk.inc.
Where do I find it?
Title: Re: String functions
Post by: LarryMc on December 04, 2008, 12:03:18 PM
Look at the footer at the bottom on all of Sapero's posts.

Larry
Title: Re: String functions
Post by: Rock Ridge Farm (Larry) on December 04, 2008, 12:06:09 PM
Thx - I thought I had installed it but had not - got it now.
Title: Re: String functions
Post by: Rock Ridge Farm (Larry) on December 04, 2008, 12:27:27 PM
Thanks to you guys I now have it working.
Moving on to my next road block. :)