Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #344 Extract filename extension

Assign to ext the fragment of the string f after the last dot character, or the empty string if f does not contain a dot.

E.g. "photo.jpg" -> "jpg"

ext must not contain the dot character.

int i = f.lastIndexOf('.');
String ext = i != -1 ? f.substring(++i) : "";
import "path/filepath"
import "strings"
ext := strings.TrimPrefix(filepath.Ext(f), ".")
sysutils
ext := ExtractFileExt(f);
if (ext <> '') then system.delete(ext,1,1);
import os
ext = os.path.splitext(f)[1][1:]
ext = File.extname(f).delete_prefix(".")

New implementation...
< >
programming-idioms.org