user1324068
user1324068

Reputation: 267

How to Split the message in as3?

Hi, am trying to split the word rtmp://xx.yyy.in/sample/test?22082208,False#&all this word.The word sample is dynamically added I don't know the count. I want to split /sample/ how to do this kindly help me?

Upvotes: 1

Views: 613

Answers (2)

francis
francis

Reputation: 6349

You want the string.split() method

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#split%28%29

var array:Array = myString.split("/"); //returns an array of everything in between / 

In your case this will return

[0]->?rtmp:/ [1]->xx.yy.in [2]->sample [3]->test?22082208,False#&all

If you're looking for everything aside from the test?22082208,False#&all part and your URL will always be in this format you can use string.lastIndexOf()

 var pos:int = string.lastIndexOf("/", 0); //returns the position of the last /
 var newString:String = string.substr(0, pos); //creates a new string starting at 0 and ending at the last index of /

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#substr%28%29

Upvotes: 2

Kodiak
Kodiak

Reputation: 5978

You can do this (and almost everything) with regex:

var input:String = "rtmp://xx.yyy.in/sample/test?22082208,False#&all";
var pattern:RegExp = /^rtmp:\/\/.*\/([^\/]*)\/.*$/;
trace(input.replace(pattern, "$1")); //outputs "sample"

Here is the regex in details:

  • ^ : start of the string
  • rtmp:\/\/ first string to find "rtmp://"
  • .* anything
  • \/ first slash
  • ([^\/]) capture everything but a slash until...
  • \/ ...second slash
  • .* anything
  • $ the end

Then $1 represents the captured group between the parenthesis.

Upvotes: 2

Related Questions