Extract file extension: Difference between revisions

m
No edit summary
 
(10 intermediate revisions by 7 users not shown)
Line 693:
=={{header|C++}}==
 
<syntaxhighlight lang="cpp">#include <stringiostream>
#include <algorithmfilesystem>
#include <iostream>
#include <vector>
#include <regex>
 
int main() {
std::string findExtension ( const std::string & filename ) {
for (std::filesystem::path file : { "picture.jpg",
auto position = filename.find_last_of ( '.' ) ;
"http://mywebsite.com/picture/image.png",
if ( position == std::string::npos )
"myuniquefile.longextension",
return "" ;
"IAmAFileWithoutExtension",
else {
"/path/to.my/file",
std::string extension ( filename.substr( position + 1 ) ) ;
"file.odd_one",
if (std::regex_search (extension, std::regex("[^A-Za-z0-9]") ))
return "thisismine." ;}) {
std::cout << file << " has extension : " << file.extension() << '\n' ;
else
}
return extension ;
}</syntaxhighlight>
}
}
 
int main( ) {
std::vector<std::string> filenames {"picture.jpg" , "http://mywebsite.com/picture/image.png" ,
"myuniquefile.longextension" , "IAmAFileWithoutExtension" , "/path/to.my/file" ,
"file.odd_one", "thisismine." } ;
std::vector<std::string> extensions( filenames.size( ) ) ;
std::transform( filenames.begin( ) , filenames.end( ) , extensions.begin( ) , findExtension ) ;
for ( int i = 0 ; i < filenames.size( ) ; i++ )
std::cout << filenames[i] << " has extension : " << extensions[i] << " !\n" ;
return 0 ;
}
</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 : !".odd_one"
"thisismine." has extension : !"."</pre></samp>
</pre>
 
 
=={{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,221 ⟶ 1,227:
document.txt_backup ->
/etc/pam.d/login ->
</pre>
 
=={{header|FutureBasic}}==
The underscores is a valid extension character in macOS and extensions are returned without leading dots.
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
void local fn DoIt
CFArrayRef paths = @[@"http://example.com/download.tar.gz",@"CharacterModel.3DS",@".desktop",@"document",@"document.txt_backup",@"/etc/pam.d/login"]
CFStringRef path
for path in paths
NSLog(@"%@",fn StringPathExtension( path ))
next
end fn
 
fn DoIt
 
HandleEvents</syntaxhighlight>
 
{{Out}}
<pre>
gz
3DS
desktop
null
txt_backup
null
</pre>
 
Line 1,255 ⟶ 1,287:
mount.ecryptfs ecryptfs
....
</pre>
 
 
=={{header|FutureBasic}}==
The underscores is a valid extension character in macOS and extensions are returned without leading dots.
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
void local fn DoIt
CFArrayRef paths = @[@"http://example.com/download.tar.gz",@"CharacterModel.3DS",@".desktop",@"document",@"document.txt_backup",@"/etc/pam.d/login"]
CFStringRef path
for path in paths
NSLog(@"%@",fn StringPathExtension( path ))
next
end fn
 
fn DoIt
 
HandleEvents</syntaxhighlight>
 
{{Out}}
<pre>
gz
3DS
desktop
null
txt_backup
null
</pre>
 
Line 1,404 ⟶ 1,409:
 
=={{header|Java}}==
<syntaxhighlight lang="java">public class Test {
import java.io.File;
</syntaxhighlight>
public static void main(String[] args) {
<syntaxhighlight lang="java">
String[] filenames = { "http://example.com/download.tar.gz",
public static void main(String[] args) {
"CharacterModel.3DS",
String[] strings = {
".desktop",
"http://example.com/download.tar.gz",
"document",
"documentCharacterModel.txt_backup3DS",
"/etc/pam.d/logindesktop",
};"document",
"document.txt_backup",
"/etc/pam.d/login",
};
for (String string : strings)
System.out.println(extractExtension(string));
}
 
static String for extractExtension(String filename : filenamesstring) {
/* we can use the 'File' class to Stringextract extthe =file-name "null";*/
File file = new File(string);
int idx = filename.lastIndexOf('.');
String filename = file.getName();
if (idx != -1) {
int String tmpindexOf = filename.substringlastIndexOf(idx'.');
if (indexOf != -1) {
if (tmp.matches("\\.[a-zA-Z0-9]+")) {
String extextension = tmpfilename.substring(indexOf);
/* and use a regex to match only }valid extensions */
if (extension.matches("\\.[A-Za-z\\d]+"))
}
System.out.println(filenamereturn + " -> " + ext)extension;
}
}
return "";
}</syntaxhighlight>
}
</syntaxhighlight>
{{Out}}
<pre>
<pre>http://example.com/download.tar.gz -> .gz
.gz
CharacterModel.3DS -> .3DS
.3DS
.desktop -> .desktop
.desktop
document -> null
 
document.txt_backup -> null
 
/etc/pam.d/login -> null</pre>
</pre>
 
=={{header|javascript}}==
Line 1,974 ⟶ 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,112 ⟶ 2,166:
</pre>
 
=={{header|Plain English}}==
The 'Extract' imperative extracts parts of a path. When extracting an extension, it starts from the last period (.) in the path string and goes until the end of the string.
<syntaxhighlight lang="text">
To run:
Start up.
Show the file extension of "http://example.com/download.tar.gz".
Show the file extension of "CharacterModel.3DS".
Show the file extension of ".desktop".
Show the file extension of "document".
Show the file extension of "document.txt_backup".
Show the file extension of "/etc/pam.d/login".
Wait for the escape key.
Shut down.
 
To show the file extension of a path:
Extract an extension from the path.
Write the extension to the console.
</syntaxhighlight>
{{out}}
<pre>
.gz
.3DS
.desktop
 
.txt_backup
.d/login
</pre>
 
=={{header|PowerShell}}==
Line 2,917 ⟶ 2,998:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./pattern" for Pattern
import "./fmt" for Fmt
 
var p = Pattern.new("/W") // matches any non-alphanumeric character
2,063

edits