[jQuery] String Parsing
this might not be jquery related, but i noticed there are a lot of savvy javascript programmers in this mailing list. 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 Thx for any help -- View this message in context: http://www.nabble.com/String-Parsing-tf2376878.html#a6622443 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
I'm no great js programmer, but that looks about right. !//-- andy matthews web developer certified advanced coldfusion programmer ICGLink, Inc. [EMAIL PROTECTED] 615.370.1530 x737 --//- -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Tombo Sent: Tuesday, October 03, 2006 10:22 AM To: discuss@jquery.com Subject: [jQuery] String Parsing this might not be jquery related, but i noticed there are a lot of savvy javascript programmers in this mailing list. 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 Thx for any help -- View this message in context: http://www.nabble.com/String-Parsing-tf2376878.html#a6622443 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
try using a regex and matching ([^\]+$) I'm not up on my \ escaping for javascript, so you may need a different number of \'s in there to make it work Tombo wrote: this might not be jquery related, but i noticed there are a lot of savvy javascript programmers in this mailing list. 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 Thx for any help ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
Tombo schrieb: 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 How about this: file1 = str1.substring( str1.lastIndexOf(\\) ); -- Jörn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
To avoid running split twice, you could do it this way (removes IE5 from compatibility however): var file1 = str1.split(\\).pop(); -blair Tombo wrote: this might not be jquery related, but i noticed there are a lot of savvy javascript programmers in this mailing list. 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 Thx for any help ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
ahhh that works nicely although i just had to add one file1=str1.substring(str1.lastIndexOf(\\)+1); J?rn Zaefferer wrote: Tombo schrieb: 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 How about this: file1 = str1.substring( str1.lastIndexOf(\\) ); -- J?rn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/String-Parsing-tf2376878.html#a6622835 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
yes. this was the elegance i was looking for. thx Blair Mitchelmore-2 wrote: To avoid running split twice, you could do it this way (removes IE5 from compatibility however): var file1 = str1.split(\\).pop(); -blair Tombo wrote: this might not be jquery related, but i noticed there are a lot of savvy javascript programmers in this mailing list. 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 Thx for any help ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/String-Parsing-tf2376878.html#a6622882 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] String Parsing
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/
Re: [jQuery] String Parsing
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/