Groups | Blog | Home
all groups > flash actionscript > june 2004 >

flash actionscript : Search String For Characters


llama79
6/18/2004 9:37:33 PM
Simply speaking first:

How do you search a string for specific characters.

Say I have a string of information that is "Josh is trying to find
information."

With an input text box I type the characters "info".

If that snippet of characters is found in the given string I want to return a
true or turn a light on on the stage.

That is not what my intentions are... but that hopefully communicates my need.
Here is what I'm doing.

I have a 2d array that is housing a set of movies. The movieClip thumbnail
list of movies is created dynamically at runtime based on this 2d array. Say I
have 25 movies one day or and 13 the next... the menu re-creates itself every
time. What I need to do now is create the menu based on a user's search
results. Say a user enters the prase, comedy. I need to search my 2d array for
all references of comedy then return the query.

I can do the above with a direct search result. But say someone types in
elepha intending to have all elephants returned. This is sort of like a Visual
Basic inString function -- I want to return all the matches that have the
characters the user enters... not only exact matches.

So far I'm setting up a for next loop to step through the array. I can find
exact matches but need to find substrings also.

Thanks,

Joshua

MechaFlasher
6/19/2004 12:04:29 AM
I had a similar situation a while ago and used this not very elegant, but
simple way of determining if a substring was part of a larger string:

var ary = new Array();
ary = myString.split(mySubstring);

Test for the length of ary. ary.length - 1 = the number of times mySubstring
was found in myString. You could probably figure out a way to apply this
method to your situation.
Gil Fewster
6/19/2004 11:55:38 AM
Have a look at the indexOf and lastIndexOf methods of the String object.

Cheers,
Gil Fewster
www.flamingmongrel.net

llama79
6/20/2004 3:27:09 AM
Thank you for the leads. I will see where they take me.

llama79
6/20/2004 4:08:57 AM
Yeah I think this is going to work perfectly. indexOf() returns a -1 if a match
is not found in a string. Since I'm searching through an array of strings I'll
just mark the indexes of the elements that have a match.

for (i=0; i<movies.length; i++) { // the .length parameter specifies the
number of elements in the array
if (movies[1].indexOf(_search)) {
trace("match found");
//break;
}
if (movies[1].indexOf(_search) == -1){
trace("match not found");
}
}

Thanks a bunch. Exactly what I needed!

Joshua
AddThis Social Bookmark Button