Extract file extension: Difference between revisions
Content added Content deleted
MaiconSoft (talk | contribs) (Added Delphi example) |
|||
Line 1,621: | Line 1,621: | ||
.txt_backup |
.txt_backup |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
As can be seen in the examples, Nim standard library function <code>splitFile</code> detects that a file such as <code>.desktop</code> is a special file. But, on the other hand, it considers that an underscore is a valid character in an extension. |
|||
<lang Nim>import os, strutils |
|||
func extractFileExt(path: string): string = |
|||
var s: seq[char] |
|||
for i in countdown(path.high, 0): |
|||
case path[i] |
|||
of Letters, Digits: |
|||
s.add path[i] |
|||
of '.': |
|||
s.add '.' |
|||
while s.len > 0: result.add s.pop() |
|||
return |
|||
else: |
|||
break |
|||
result = "" |
|||
for input in ["http://example.com/download.tar.gz", "CharacterModel.3DS", |
|||
".desktop", "document", "document.txt_backup", "/etc/pam.d/login"]: |
|||
echo "Input: ", input |
|||
echo "Extracted extension: ", input.extractFileExt() |
|||
echo "Using standard library: ", input.splitFile()[2] |
|||
echo()</lang> |
|||
{{out}} |
|||
<pre>Input: http://example.com/download.tar.gz |
|||
Extracted extension: .gz |
|||
Using standard library: .gz |
|||
Input: CharacterModel.3DS |
|||
Extracted extension: .3DS |
|||
Using standard library: .3DS |
|||
Input: .desktop |
|||
Extracted extension: .desktop |
|||
Using standard library: |
|||
Input: document |
|||
Extracted extension: |
|||
Using standard library: |
|||
Input: document.txt_backup |
|||
Extracted extension: |
|||
Using standard library: .txt_backup |
|||
Input: /etc/pam.d/login |
|||
Extracted extension: |
|||
Using standard library: </pre> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |