Text search in flash

I have a small problem, i’m trying to write a function that searches the text stored in a variable for a word (that will be the only function argument). For example i have a variable with the text “This is a beautiful day” and i want to search for the word “day”. I don’t think there’s a way of separating the varibale into several tokens in Flash. Any suggestions ??

Try this - it’s case sensitive. Convert the strings to lower case or something if you don’t want a case sensitive search

Ok, small problem again :). The file must be corrupted or something, because i download it, and when i try to open it flash shows 'Unexpected file format". Strange huh ?:slight_smile:
Can u post the file again plz ?

This is a flash 5 version the other was mx

Here’s the MX one again - just incase it was corrupted - because I had probs submitting it

Works great, thanks ;), by the way, it is the function indexOf(wordtosearch) that makes the entire job right ? So easy :).

Yes - if the return is not -1 then the word is there just do a check to see

if (sentence.indexOf(word) != -1) { // word is there
// do whatever
}

Hi there, correct me if i’m wrong, but that function allows you to determine if a given value is part of the array right ? That means that the information stored in the variable is a vector ? Like sentence[0] gives you the first element of the string and so on ? Is this correct ?

Well, we had a little chat about strings being like arrays or not, and this is a case where they’re not.

sentence[0] won’t give anything, except maybe the first character. And no, I just tried,

sentence="alala ll";
trace (sentence[0]);

returns undefined.

Instead, you can split your string into an array…

pom :elderly:

Split a string into an array ? How can i do that ? Is there any function to divide the string into several tokens ??

Yes, you can’t use indexOf for an array. As pom said

sentence = “dude” just means to you are defining a string.

If you trace sentence[0] flash would look for the array sentence, then the first element, which obviously doesn’t exist.

whereas

sentence = [ “dude”, “dudette” ]; would define an array.

I’ll look into the code for your question - I’ve just woken up…!

split() the string into an array. Then search the array.

split() ? If use sentence.split();, what will happen ? It divides itself into an array ??

sentence="I am so cool";
result=sentence.split(" ");
trace (result);

result in an array containing the words of the string:

trace (result[0]);
//returns "I" and so on

pom :cowboy:

Yes. Use a for loop to search from sentence[0] to sentence.length for the word you’re looking for.