Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-19-2012, 05:58 PM   #1
mexicanbean
Registered User
 
Join Date: Oct 2008
Posts: 15
Default help with a string to array function

Im looking for a string function similar to split, i need to find text within a string and store it in an array...

For example..

Useless text i dont need
[start]this is the text i need to find and store[finish]
useless text i dont need
[start]store and find this too[finish]
useless text...

so locate all text within the [start] and [finish] and add it into an array.
so the above string would produce

array[0]=this is the text i need to find and store;
array[1]=store and find this too;

Is there such a function?

Thanks.

Last edited by mexicanbean; 07-19-2012 at 06:37 PM.
mexicanbean is offline   Reply With Quote
Old 07-19-2012, 10:32 PM   #2
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,184
Default

Here's a quick function to split string to array by begin and end key strings:

ActionScript Code:
var someText:String = "Useless text i dont need\n[start]this is the text i need to find and store[finish]\nuseless text i dont need\n[start]store and find this too[finish]\nuseless text..."; var array:Array = splitByKeys(someText, "[start]", "[finish]"); trace(array[0]); // this is the text i need to find and store trace(array[1]); // store and find this too function splitByKeys(string:String, begin:String, end:String):Array {     if(begin.length + end.length == 0) return string.split("");     var i:int = 0;     var j:int = 0;     var round:int = 0;     var array:Array = [];     while(true)     {         i = string.indexOf(begin, (round++ != 0 ? j + end.length : i));         j = string.indexOf(end, i + begin.length);         array.push(string.substring(i + begin.length, j));         if(i == -1 || j == -1) break;     }     array.pop();     if(array.length == 0) throw new Error("parameter \"string\" doesn't contain both keys!");     return array; }

Will throw and error if string doesn't contain begin and end key strings.

Last edited by -:)lauri; 07-19-2012 at 10:51 PM. Reason: I corrected the code for "i" to find begin key also from the beginning of the string
-:)lauri is offline   Reply With Quote
Old 07-19-2012, 11:11 PM   #3
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,184
Default

oh it was as2... well to get it work with as2 change these

var i:int = 0;
var j:int = 0;
var round:int = 0;

to

var i:Number = 0;
var j:Number = 0;
var round:Number = 0;
-:)lauri 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 Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:26 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.