# Text search in flash

**URL:** <https://forum.kirupa.com/t/text-search-in-flash/6262>\
**Category:** flash\
**Created:** [October 2, 2002, 10:23pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262 "2002-10-02T22:23:11Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmarnet](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@pmarnet](https://forum.kirupa.com/u/pmarnet)\
**Post date:** [October 2, 2002, 10:23pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/1 "2002-10-02T22:23:11Z")

</div>

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 ??

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 2, 2002, 10:49pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/2 "2002-10-02T22:49:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 2, 2002, 10:55pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/3 "2002-10-02T22:55:05Z")

</div>

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 ?🙂  
Can u post the file again plz ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 2, 2002, 10:57pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/4 "2002-10-02T22:57:15Z")

</div>

This is a flash 5 version the other was mx

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 2, 2002, 10:57pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/5 "2002-10-02T22:57:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 2, 2002, 11:06pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/6 "2002-10-02T23:06:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 2, 2002, 11:19pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/7 "2002-10-02T23:19:54Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 10:12am UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/8 "2002-10-03T10:12:02Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 11:48am UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/9 "2002-10-03T11:48:58Z")

</div>

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,

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

```

returns undefined.

Instead, you can split your string into an array…

pom :elderly:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 12:22pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/10 "2002-10-03T12:22:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 12:47pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/11 "2002-10-03T12:47:21Z")

</div>

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…!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 1:43pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/12 "2002-10-03T13:43:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 2:10pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/13 "2002-10-03T14:10:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 2:52pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/14 "2002-10-03T14:52:36Z")

</div>

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

```

result in an array containing the words of the string:

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

```

pom :cowboy:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 3, 2002, 2:55pm UTC](https://forum.kirupa.com/t/text-search-in-flash/6262/15 "2002-10-03T14:55:15Z")

</div>

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