Find and Replace string values
I need to be able to check a string for substrings between 2 delimiter characters, and the rewrite the string.
I can do this with out any problems if i know the values. But i need to be able to check for any number of substrings.
some word here and #string1# and some more #string 2# #another string#
How could i check the values between the delimiter characters?
How would the function work?
I hope that makes sense, and thank you for you help
So your input might be something like this:
The quick brown #animal1# jumps over the lazy, sleeping #animal2#.
You want to return:
I'm sure you could do this with regular expressions to trap anything between your deliminters, but I'm not that familar with reg exp so I can't help you on that front. I can tell you that what I would do is use inStr, Len and Mid to find if the deliminator is present and if so, what the location of the first one is. Trim your string to the first deliminater using the length minus the position of the first instance of the deliminator. Now you should have a string like:
animal1# jumps over the lazy, sleeping #animal2#.
Now find the location of the first deliminater in the above string the same way, except we want both the substring before the deliminater (probably best to put this in an array) and then we want to again, trim off what's after so we get:
jumps over the lazy, sleeping #animal2#.
(note there is a space before jumps).
Since we don't know how many substrings there will be, we're going to have to loop through the string until inStr doesn't find the deliminator any more.
What you should be left with is an array with your values which you can then loop use as needed.
This type of process can be tricky, you'll need to modify the deliminator and remaining length to account for the difference between position of a letter and where you actually want to trim (usually adding and subtracting 1 to move the position of your Mid statements). When I do this, I typically use response.write a lot to see what is actually going on (what position am I at, what does the remaining string look like, what does my substring look like, etc).
Adding string parsing to your skillset is very valuable, though. Once you can combine variables into a single string that you can then parse later, it makes passing data between scripts much easier.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)