View Full Version : how to split a string in log file

02-03-2009, 04:47 AM

could anyone help me.how i can split this string"i am new to this" using the white space delimiter.

and how should i declare this string before i can paste it in Flex?

thank u so much...

02-03-2009, 04:51 AM
Huh, my English is not that good :D Could you maybe give a concrete example? How should the output look like and what do you mean by "how should I declare this string before I can paste it in Flex" ? Paste what into Flex ? Why ? and what do you understand by "paste into Flex" because you can "load data into Flex" or "pass data to Flex" but not "paste data"...

02-03-2009, 05:30 AM
var myStr:String = "i am new to this";
var myArray:Array = myStr.split(" ");
var myNewStr:String=myArray[0]+"-"+myArray[1]+"-"+myArray[2];

02-04-2009, 01:44 AM
i'm sorry for the inconvenience...:eek:

ok,first: I got this line of codes in flex;
var queryStr:String = ' - - [15/Sep/2008:10:37:14 -0700];
var params:Array = queryStr.split(" ");

i want to split them into array like this: [15/Sep/2008:10:37:14 -0700]
i know that i can use the "white space" as a delimiter in this case,but what i want to know is,where should i put the above codes?whether it should be in the <mx:script> or what??

second:do i have to declare an array before i can use this codes?

thank you so much in advance...;)

02-04-2009, 05:13 AM
ok if this is ur string
var queryStr:String = ' - - [15/Sep/2008:10:37:14 -0700]
than what u want on out output...?

02-04-2009, 06:29 AM
i want to output this string " - - [15/Sep/2008:10:37:14 -0700]" into two columns using the split function.can it be done?


02-04-2009, 08:49 AM
Ever considered simply doing this ?

var st:String = " - - [15/Sep/2008:10:37:14 -0700]";
var arr:Array = st.split(" - - ");
trace("value1: " + arr[0] + "\n" + "value2: " + arr[1]);

02-04-2009, 09:09 AM
<?xml version="1.0" encoding="utf-8"?>
<!-- SplitExample -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
private function applicationCompleteHandler():void
var stringToSplit:String = " - - [15/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - - - [16/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - - - [17/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - - - [18/Sep/2008:10:37:14 -0700]";
stringToSplit += " - - - - [19/Sep/2008:10:37:14 -0700] - - ";
var re:RegExp = /(?P<a>[^\2]+?)(\s\-\s\-\s)(?P<b>[^\2]+?)\2/g;
var result:Array;
result = re.exec(stringToSplit);
if (result)
sourceArray.push({ a: result.a, b: result.b });

while (result);
<mx:DataGrid width="100%" height="100%">
<mx:ArrayCollection id="dp">
<mx:Array id="sourceArray"/>
<mx:DataGridColumn dataField="a"/>
<mx:DataGridColumn dataField="b"/>
But that would be much easier if you could pick up another delimiter.

EDIT: haha, oh, sorry, what was I thinking when I wrote that! :) Sure, just use Array.split(), what I've written is surely an overkill :)

02-04-2009, 09:38 AM
^ Hahah, still, quite nice, nice. ;) I always skipped classes where they were talking about RegExp... but still, RegExp si really great and although I'm not great at it I still use it from time to time.

02-04-2009, 11:41 PM
what you have done is really really great!!:)thanks a lot...it's really above my level..i'm just a beginner in exploring Flex.

u have solve my problem..just a little wonder when i use this code below;

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
var st:String = " - - [15/Sep/2008:10:37:14 -0700]";
var arr:Array = st.split(" - - ");

<mx:Label text="{arr}" id="myLabel"/>

the output;,[15/Sep/2008:10:37:14 -0700]

why there is a "," there when i don't even define it in code??

02-05-2009, 07:59 AM
This is how Array.toString() works it joins the results with commas. You can alter it like this:
<mx:String id="foo"></mx:String>
<mx:Label text="The delimited string is:{foo} The result is: {arr.join(foo)}" id="myLabel"/>

02-05-2009, 08:30 AM
Or you can manually build the desired string by adding items from the array together ( just like in the example I posted; maybe you'll run over a situation when you don't want to display the elements of the entire array at once ).