开发者

AS3 split string problem

I am not very familiar with AS3 but I am trying to split a string using: root.loaderInfo.loaderURL. This works fine it returns me the url now I am trying to split it below is an example of the url:

http://cw-pdevprt-05.tm-gnet.com:10040/wps/myportal/Ad_Sales_Test/!ut/p/c5/04_SB8K8xLLM9MSSzPy8xBz9CP0os3gzQ28zD0sTQ0t3DwtzA89g_yATTxM_QwMTI_1wkA6zeGd3Rw8Tcx8DAwsXNwsDIydTM89AAxcDA09TiLwBDuBooO_nkZ-bql-QnZ3m6KioCAD3_3C4/dl3/d3/L2dBISEvZ0FBIS9nQSEh/

Long and bloated I know but I have no control over this anyway after the spliting the string I am just trying to be left with :

cw-pdevprt-05.tm-gnet.com:10040

I am able to remove the https:// but can not remove the part starting with "/", I have used the following code:

var urlLocation:String = "http://cw-pdevprt-05.tm-gnet.com:10040/wps/myportal/Ad_Sales_Test/!ut/p/c5/04_SB8K8xLLM9MSSzPy8xBz9CP0os3gzQ28zD0sTQ0t3DwtzA89g_yATTxM_QwMTI_1wkA6zeGd3Rw8Tcx8DAwsXNwsDIydTM89AAxcDA09TiLwBDuBooO_nkZ-bql-QnZ3m6KioCAD3_3C4/dl3/d3/L2dBISEvZ0FBIS9nQSEh/";
var urlArray:Array = urlLocation.split("http://");
var urlSecond:String = urlArray;
var secondArray = urlSecond.split("/");

I tried the above but it always fails when trying to split the second part, can anyone help me get the cor开发者_开发问答rect outcome ?

Thanks in advance


you can try with this code (regexp) :

var domain:String = urlLocation.replace(/^https?:\/\/([^\/]+).*$/i, '$1');


url = url.substr( 7 ); // removes the "http://" - use 8 if it's "https://"
url = url.substring( 0, url.indexOf( "/" ) ); // url is now "cw-pdevprt-05.tm-gnet.com:10040"


I believe it is because you are trying to put an array into a string, that's what a string is, but you have an array of strings, where as the first is an array of characters. Either way, I doubt the compiler would like it.

However, I fail to understand why you're doing all those steps, you should be able to do it all with one (or two) lines of code (not 100% sure if the following will work in AS is why):

 var urlGet:String = urlLocation.split("/")[x]; //Not sure on all the details of .split() in AS. So "x" could be anything from 1-3 depending on whether it initializes are zero, and whether or not it will 'split at "//" to form an empty space in the array, or just skip over it. I imagine (hope) it would create an empty, but I don't want to say for certain

If the above doesn't work than...

 var urlGet:Array = urlLocation.split("/")
 var urlGet2:String = urlGet[x]  //This is x for similar reasons listed above. 

Hope that helps

Edit: Just to clarify,

  var urlGet:String = urlLocation.split("/")[x]

Is telling ActionScript to split the string, and than set urlGet equal to the xth element in the created array. This is valid code in Python, just not sure if it would work here.


The Flex class URLUtil will make this simple for you.

var servername:String = URLUtil.getServerName(urlLocation);

You don't have to use the whole Flex framework to use URLUtil, you can copy the class and move it into your src directory.

http://dl.dropbox.com/u/672147/URLUtil.as

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜