Extract file extension: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Tcl}}: Added zkl) |
||
Line 492: | Line 492: | ||
assert {[file ext $file] eq $ext} |
assert {[file ext $file] eq $ext} |
||
}</lang> |
}</lang> |
||
=={{header|zkl}}== |
|||
The File object has a method splitFileName that does just that, returning a list of the parts. The method knows about the OS it was compiled on (Unix, Windows). |
|||
<lang zkl>T("picture.jpg","http://mywebsite.com/picture/image.png", |
|||
"myuniquefile.longextension","IAmAFileWithoutExtension", |
|||
"/path/to.my/file","file.odd_one").apply(File.splitFileName).println();</lang> |
|||
{{out}} |
|||
<pre> |
|||
L(L("","","picture",".jpg"), |
|||
L("","http://mywebsite.com/picture/","image",".png"), |
|||
L("","","myuniquefile",".longextension"), |
|||
L("","","IAmAFileWithoutExtension",""), |
|||
L("","/path/to.my/","file",""), |
|||
L("","","file",".odd_one")) |
|||
</pre> |
|||
The last too are odd ducks so some code is in order: |
|||
<lang zkl>fcn exonly(fileName){ |
|||
var re=RegExp(0'|\.[a-zA-Z0-9]+$|); |
|||
ext:=File.splitFileName(fileName)[-1]; |
|||
if(not re.matches(ext)) return(""); |
|||
ext |
|||
}</lang> |
|||
<lang zkl>T("picture.jpg","http://mywebsite.com/picture/image.png", |
|||
"myuniquefile.longextension","IAmAFileWithoutExtension", |
|||
"/path/to.my/file","file.odd_one").apply(exonly).println();</lang> |
|||
{{out}} |
|||
<pre>L(".jpg",".png",".longextension","","","")</pre> |