Extract file extension: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 381: | Line 381: | ||
return exitcode; |
return exitcode; |
||
}</lang> |
}</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang [[C sharp|C#]]>public static string FindExtension(string filename) { |
|||
int indexOfDot = filename.Length; |
|||
for (int i = filename.Length - 1; i >= 0; i--) { |
|||
char c = filename[i]; |
|||
if (c == '.') { |
|||
indexOfDot = i; |
|||
break; |
|||
} |
|||
if (c >= '0' && c <= '9') continue; |
|||
if (c >= 'A' && c <= 'Z') continue; |
|||
if (c >= 'a' && c <= 'z') continue; |
|||
break; |
|||
} |
|||
//The dot must be followed by at least one other character, |
|||
//so if the last character is a dot, return the empty string |
|||
return indexOfDot + 1 == filename.Length ? "" : filename.Substring(indexOfDot); |
|||
}</lang> |
|||
'''Using regular expressions (C# 6)''' |
|||
<lang [[C sharp|C#]]>public static string FindExtension(string filename) => Regex.Match(filename, @"\.[A-Za-z0-9]+$").Value;</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 423: | Line 446: | ||
thisismine. has extension : ! |
thisismine. has extension : ! |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang [[C sharp|C#]]>public static string FindExtension(string filename) { |
|||
int indexOfDot = filename.Length; |
|||
for (int i = filename.Length - 1; i >= 0; i--) { |
|||
char c = filename[i]; |
|||
if (c == '.') { |
|||
indexOfDot = i; |
|||
break; |
|||
} |
|||
if (c >= '0' && c <= '9') continue; |
|||
if (c >= 'A' && c <= 'Z') continue; |
|||
if (c >= 'a' && c <= 'z') continue; |
|||
break; |
|||
} |
|||
//The dot must be followed by at least one other character, |
|||
//so if the last character is a dot, return the empty string |
|||
return indexOfDot + 1 == filename.Length ? "" : filename.Substring(indexOfDot); |
|||
}</lang> |
|||
'''Using regular expressions (C# 6)''' |
|||
<lang [[C sharp|C#]]>public static string FindExtension(string filename) => Regex.Match(filename, @"\.[A-Za-z0-9]+$").Value;</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 452: | Line 452: | ||
=> |
=> |
||
"txt"</lang> |
"txt"</lang> |
||
=={{header|Emacs Lisp}}== |
|||
<lang Lisp>(file-name-extension "foo.txt") |
|||
=> |
|||
"txt"</lang> |
|||
No extension is distinguished from empty extension but an <code>(or ... "")</code> can give <code>""</code> for both if desired |
|||
<lang Lisp>(file-name-extension "foo.") => "" |
|||
(file-name-extension "foo") => nil</lang> |
|||
An Emacs backup <code>~</code> or <code>.~NUM~</code> are not part of the extension, but otherwise any characters are allowed. |
|||
<lang Lisp>(file-name-extension "foo.txt~") => "txt" |
|||
(file-name-extension "foo.txt.~1.234~") => "txt"</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 542: | Line 525: | ||
/etc/pam.d/login -> |
/etc/pam.d/login -> |
||
</pre> |
</pre> |
||
=={{header|Emacs Lisp}}== |
|||
<lang Lisp>(file-name-extension "foo.txt") |
|||
=> |
|||
"txt"</lang> |
|||
No extension is distinguished from empty extension but an <code>(or ... "")</code> can give <code>""</code> for both if desired |
|||
<lang Lisp>(file-name-extension "foo.") => "" |
|||
(file-name-extension "foo") => nil</lang> |
|||
An Emacs backup <code>~</code> or <code>.~NUM~</code> are not part of the extension, but otherwise any characters are allowed. |
|||
<lang Lisp>(file-name-extension "foo.txt~") => "txt" |
|||
(file-name-extension "foo.txt.~1.234~") => "txt"</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 1,475: | Line 1,474: | ||
document.txt_backup '' |
document.txt_backup '' |
||
/etc/pam.d/login '' |
/etc/pam.d/login '' |
||
</pre> |
|||
=={{header|Perl 6}}== |
|||
The built-in <code>IO::Path</code> class has an <code>.extension</code> method: |
|||
<lang perl6>say $path.IO.extension;</lang> |
|||
Contrary to this task's specification, it |
|||
* doesn't include the dot in the output |
|||
* doesn't restrict the extension to letters and numbers. |
|||
Here's a custom implementation which does satisfy the task requirements: |
|||
<lang perl6>sub extension (Str $path --> Str) { |
|||
$path.match(/:i ['.' <[a..z0..9]>+]? $ /).Str |
|||
} |
|||
# Testing: |
|||
printf "%-35s %-11s %-12s\n", $_, extension($_).perl, $_.IO.extension.perl |
|||
for < |
|||
http://example.com/download.tar.gz |
|||
CharacterModel.3DS |
|||
.desktop |
|||
document |
|||
document.txt_backup |
|||
/etc/pam.d/login |
|||
>;</lang> |
|||
{{out}} |
|||
<pre> |
|||
http://example.com/download.tar.gz ".gz" "gz" |
|||
CharacterModel.3DS ".3DS" "3DS" |
|||
.desktop ".desktop" "desktop" |
|||
document "" "" |
|||
document.txt_backup "" "txt_backup" |
|||
/etc/pam.d/login "" "" |
|||
</pre> |
</pre> |
||
Line 1,751: | Line 1,712: | ||
document.txt_backup | |
document.txt_backup | |
||
/etc/pam.d/login | |
/etc/pam.d/login | |
||
</pre> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
The built-in <code>IO::Path</code> class has an <code>.extension</code> method: |
|||
<lang perl6>say $path.IO.extension;</lang> |
|||
Contrary to this task's specification, it |
|||
* doesn't include the dot in the output |
|||
* doesn't restrict the extension to letters and numbers. |
|||
Here's a custom implementation which does satisfy the task requirements: |
|||
<lang perl6>sub extension (Str $path --> Str) { |
|||
$path.match(/:i ['.' <[a..z0..9]>+]? $ /).Str |
|||
} |
|||
# Testing: |
|||
printf "%-35s %-11s %-12s\n", $_, extension($_).perl, $_.IO.extension.perl |
|||
for < |
|||
http://example.com/download.tar.gz |
|||
CharacterModel.3DS |
|||
.desktop |
|||
document |
|||
document.txt_backup |
|||
/etc/pam.d/login |
|||
>;</lang> |
|||
{{out}} |
|||
<pre> |
|||
http://example.com/download.tar.gz ".gz" "gz" |
|||
CharacterModel.3DS ".3DS" "3DS" |
|||
.desktop ".desktop" "desktop" |
|||
document "" "" |
|||
document.txt_backup "" "txt_backup" |
|||
/etc/pam.d/login "" "" |
|||
</pre> |
</pre> |
||