Extract file extension: Difference between revisions
m
→{{header|EasyLang}}
Jazzedpineda (talk | contribs) (Add Plain English) |
|||
(8 intermediate revisions by 5 users not shown) | |||
Line 693:
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <
#include <
int main() {
for (std::filesystem::path file : { "picture.jpg",
"http://mywebsite.com/picture/image.png",
"myuniquefile.longextension",
"IAmAFileWithoutExtension",
"/path/to.my/file",
"file.odd_one",
std::cout << file << " has extension : " << file.extension() << '\n' ;
}
}</syntaxhighlight>
{{out}}
<samp><pre>"picture.jpg" has extension : ".jpg
"http://mywebsite.com/picture/image.png" has extension : ".png
"myuniquefile.longextension" has extension : ".longextension
"IAmAFileWithoutExtension" has extension :
"/path/to.my/file" has extension :
"file.odd_one" has extension :
"thisismine." has extension :
=={{header|Clojure}}==
Line 947 ⟶ 929:
document.txt_backup
/etc/pam.d/login</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func isalphanum c$ .
c = strcode c$
return if c >= 65 and c <= 90 or c >= 97 and c <= 122 or c >= 48 and c <= 57
.
func$ exext path$ .
for i = len path$ downto 1
c$ = substr path$ i 1
if isalphanum c$ = 1
ex$ = c$ & ex$
elif c$ = "."
return ex$
else
break 1
.
.
.
for s$ in [ "http://example.com/download.tar.gz" "CharacterModel.3DS" ".desktop" "document" "document.txt_backup" "/etc/pam.d/login" ]
print s$ & " -> " & exext s$
.
</syntaxhighlight>
=={{header|Emacs Lisp}}==
Line 1,191 ⟶ 1,196:
/etc/pam.d/login (empty string)
</pre>
=={{header|Frink}}==
Line 1,403 ⟶ 1,409:
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.File;
</syntaxhighlight>
<syntaxhighlight lang="java">
public static void main(String[] args) {
String[] strings = {
"http://example.com/download.tar.gz",
"document.txt_backup",
"/etc/pam.d/login",
};
for (String string : strings)
System.out.println(extractExtension(string));
}
static String
/* we can use the 'File' class to
File file = new File(string);
String filename = file.getName();
int
if (indexOf != -1) {
String
/* and use a regex to match only
if (extension.matches("\\.[A-Za-z\\d]+"))
}
return "";
}
</syntaxhighlight>
{{Out}}
<pre>
.gz
.3DS
.desktop
</pre>
=={{header|javascript}}==
Line 1,973 ⟶ 1,987:
null ok
></pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program Extract_file_extension;
{FreePascal has the built-in function ExtractFileExt which returns the file extension.
* it does need charachters before the period to return the proper extension and it returns
* the extension including the period}
Uses character,sysutils;
Const arr : array of string = ('http://example.com/download.tar.gz','CharacterModel.3DS','.desktop',
'document','document.txt_backup','/etc/pam.d/login');
Function extractextension(fn: String): string;
Var
i: integer;
Begin
fn := 'prefix' + fn; {add charachters before the period}
fn := ExtractFileExt(fn);
For i := 2 to length(fn) Do {skip the period}
If Not IsLetterOrDigit(fn[i]) Then exit('');
extractextension := fn;
End;
Var i : string;
Begin
For i In arr Do
writeln(i:35,' -> ',extractextension(i))
End.
</syntaxhighlight>
{{out}}
<pre>
http://example.com/download.tar.gz -> gz
CharacterModel.3DS -> 3DS
.desktop -> desktop
document ->
document.txt_backup ->
/etc/pam.d/login ->
</pre>
=={{header|Perl}}==
Line 2,943 ⟶ 2,998:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./fmt" for Fmt
var p = Pattern.new("/W") // matches any non-alphanumeric character
|