Extract file extension: Difference between revisions

Content added Content deleted
(obsolete)
(add Standard ML)
Line 2,231: Line 2,231:
/etc/pam.d/login -> </pre>
/etc/pam.d/login -> </pre>
Note: the task's description seems wrong to me; on a Unix machine, files beginning with "." are treated as hidden files (eg. in ls) and the suffix can be considered to be empty. As opposed to "a.desktop".
Note: the task's description seems wrong to me; on a Unix machine, files beginning with "." are treated as hidden files (eg. in ls) and the suffix can be considered to be empty. As opposed to "a.desktop".

=={{header|Standard ML}}==

This just demonstrates how to functionally extend the built-in function to the alpha-numeric restriction. Since file names starting with '.' are supposed to be "hidden" files in Unix, they're not considered as an extension.
<lang sml>fun fileExt path : string =
getOpt (Option.composePartial (Option.filter (CharVector.all Char.isAlphaNum), OS.Path.ext) path, "")

val tests = [
"http://example.com/download.tar.gz",
"CharacterModel.3DS",
".desktop",
"document",
"document.txt_backup",
"/etc/pam.d/login"
]

val () = app (fn s => print (s ^ " -> \"" ^ fileExt s ^ "\"\n")) tests</lang>
{{out}}
<pre>http://example.com/download.tar.gz -> "gz"
CharacterModel.3DS -> "3DS"
.desktop -> ""
document -> ""
document.txt_backup -> ""</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==