Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-10-2009, 01:16 PM   #1
anndorian
Member
 
Join Date: Nov 2004
Posts: 79
Question Find positions in a string.

I have a function that finds the position of a specific string.

ActionScript Code:
firstPos = string.indexOf(searchText); lastPos = firstPos+string.width;

But indexOf searches the string and returns the position of the first occurrence.
I know there's a startIndex parameter but I dont know how to use it so that I could go through an entire string.

Like for example: " Hello there little boy. What's your name boy?"

How can I get the position of first boy word and then the position of second boy word ?

Thanks in advance.
anndorian is offline   Reply With Quote
Old 11-10-2009, 04:59 PM   #2
nunomira
Senior Member
 
nunomira's Avatar
 
Join Date: Mar 2003
Location: portugal
Posts: 305
Default

hi,

This should do it:
ActionScript Code:
function findPositions (word:String, sentence:String):Array {     var positions:Array = new Array();     var fromIndex:uint = 0;     var foundIndex:uint;     while ( sentence.indexOf (word, fromIndex) > -1 )     {         foundIndex = sentence.indexOf(word,fromIndex);         positions.push (foundIndex);         fromIndex = foundIndex + word.length;     }     return positions; } trace (findPositions ("boy", "Hello there little boy. What's your name boy?" ) );
nunomira is offline   Reply With Quote
Old 11-10-2009, 05:35 PM   #3
anndorian
Member
 
Join Date: Nov 2004
Posts: 79
Default

Thanks ! This helps a lot ! Here's a cookie. :]
anndorian is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:43 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.