1

嗨,我正在尝试拆分单词rtmp://xx.yyy.in/sample/test?22082208,False#&all这个单词。单词样本是动态添加的,我不知道计数。我想拆分/sample/如何做到这一点请帮助我?

4

2 回答 2

2

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

于 2012-04-10T13:10:41.117 回答
2

您可以使用正则表达式执行此操作(以及几乎所有操作):

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

这是详细的正则表达式:

  • ^: 字符串的开始
  • rtmp:\/\/找到“rtmp://”的第一个字符串
  • .*任何事物
  • \/第一个斜线
  • ([^\/])捕获除斜线之外的所有内容,直到...
  • \/...第二个斜线
  • .*任何事物
  • $结束

然后$1在括号之间表示捕获的组。

于 2012-04-10T13:10:50.280 回答