Friday, August 20, 2010

Searching text in a string using AS3: Way 1

In this tutorial we will learn how to search and find a substring in a string using indexes.

To find a piece of text in a string you can use indexOf() and lastIndexOf() methods. Each of them returns the index (position) of the first symbol of the substring in the string. What's the differene? IndexOf() starts the search from the beginning to the end of the string, while lastIndexOf - from the end to the beginning. If a substring isn't found, -1 is returned.

IndexOf() method has 2 parameters: substring and startIndex (optional).

Here's an example:

var str:String = "This guy is good at telling jokes. Really good."

var index:int = str.indexOf("good");

if(index != -1){
trace("This string contains the word good at " + index);
}

Output:
This string contains the word good at 12

That means the first letter of the substring ("g") has the 12th position in the string. We can set the second parameter for our method - startIndex, to search from that point:

var str:String = "This guy is good at telling jokes. Really good."

var index:int = str.indexOf("good", 13);

if(index != -1){
trace("This string contains the word good at " + index);
}

Output:
This string contains the word good at 42

We can use while statement to get all of the substrings:

var str:String = "This guy is good at telling jokes. Really good. Yeah, good."
var index:int=-1
while ( (index = str.indexOf("good", index+1) ) !=-1){
trace("This string contains the word good at " + index);
}

Now, that code above might seem complicated, but it really isn't. Take a look:

First we set up the string and index variable. We put it as -1, because later (in the while statement) we will add 1 to it, making it 0.

index = str.indexOf("good", index+1);

We add 1 to the index to check for more substrings after the one we just found. And if we don't find it, just stop looking. That's why we use the while loop. It's not that hard when you understand it.

Keep in mind that this kind of search is case sensitive. This means that if you search for "good' and the text has "Good" in it, this won't count as the same word. We can use toLowerCase for this.

var str:String = "Good! This guy is good at telling jokes. Really good. Yeah, good. GOOD!"
var index:int=-1
var str_lower=str.toLowerCase();
while ( (index = str_lower.indexOf("good", index+1) ) !=-1){
trace("This string contains the word good at " + index);
}

Thanks for reading!

Related:

Searching text in a string using AS3: Way 2

4 comments:

Jonathan said...

Thanks for this. Just what I needed.

Anonymous said...

MyComent?

Headhunter said...

heyy...awesome tutorial ..just what i needed...can you plzzzzzzzzz upload a tutorial on how to create a searchable xml database.. without using any components... pleeeaasee... i have been searching this for months...but cant find it anywhere.... if you make one...i will never ever ever ever forget you in my life....

kenzie jones said...

I Can't understand that tutorial. Its really confusing for me. I think its not help me.
what is a digital signature

Post a Comment