views:

1016

answers:

3

I am finding this task challenging in AS3.

Excuse me if I am missing something basic/simple or some builtin method which can do this. I am not well versed with RegExp.

I have a DYNAMIC string representing full file path which looks exactly like "d:\temp\abc.doc". I want to extract the filename part from complete string e.g. abc.doc.

I am aware of techniques like using "fullPath.lastIndexOf("\\")" or regular expressions. The problem is it doesn't seem to work if you have "\" in your path. It works with "/". The path being dynamic can't be manipulated to replace "\" with "/" or any other delimiter. My interpretation is that since "\" is used to escape characters - ANY character appearing AFTER "\" is ignored by actionscript. e.g.

var fullPath:String = "A\B\C";

trace(fullPath.length); //**RETURNS 3** since "\B" & "\C" 
                        // are being treated as SINGLE chars

var bSlash:int =  fullPath.lastIndexOf("\\") //RETURNS **-1**
A: 

I think if use use valueOf property on your string to get a primitive string, you might get what you want. Actionscript 3 generally creates a big fancy string object which does magical things with escape characters, but a primitive string is more like a JavaScript/ECMA string and the backslashes should show up as separate characters. Haven't tested this, but try it out.

apphacker
+6  A: 

You need to use A\\B\\C (as in most languages, in AS a string literal with an unescaped \ creates an escape sequence by combining with the next character). I have a feeling that you are not generating the dynamic string properly. If the string is supposed to be a path then it should contain a separate \ character in between path fragments by escaping the backslash using \\.

dirkgently
A: 

As dirkgently has pointed out, the issue is using string value instead of string literal. This link helped me sorting this out.

Pradeep Kalugade