nice.  i like that regular expression example.  but i agree that the split
method is probably quicker.  thanks


Christof Donat wrote:
> 
> Hi,
> 
>> i want to grab just the filename and extension from the following string:
>>
>> str1="F:\\Test1\\Test2\\Test3\\test.txt";
>>
>> i want to grab "test.txt"
>>
>> i use this code:
>>
>> file1=(str1.split("\\"))[(str1.split("\\")).length-1];
>>
>> i was wondering if there is a better way to grab that part of the string
> 
> Yes, of course:
> 
> var tmp = str1.split("\\");
> file1 = tmp[tmp.length-1];
> 
> That way the JS-interpreter doesn't need to split the string twice. I
> think 
> that this is the fastest aproach.
> 
> Another aproach could be:
> 
> file1 = str1;
> var tmp;
> while( (tmp = file1.indexOf("\\")) >= 0 ) file1 = file1.substr(tmp+1);
> 
> But I think, that is slower. The advantage is that it is easy to
> understand 
> that you ar working on the string.
> 
> Yet another aproach:
> 
> file1 = str1.match(/[^\\]*$/);
> 
> I guess this can compete in performance with the split()-method - shold be 
> just a bit slower since the evaluation of a regular expression is more 
> complex than a simple split.
> It is the shortest variant, but not the most readable.
> 
> If you ask me I would either recomend the split()-method or the 
> regular-expression-method.
> 
> Christof
> 
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
> 
> 

-- 
View this message in context: 
http://www.nabble.com/String-Parsing-tf2376878.html#a6623119
Sent from the JQuery mailing list archive at Nabble.com.


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to