함수 get extension()

1 개요[ | ]

get file extension()
get extension()
GetExtension()
  • 파일명에서 확장자 추출하는 함수
  • 예: "C:\mydir.old\myfile.ext" → "ext" 또는 ".ext"

2 Bash[ | ]

filename='/home/run.sh'
extension="${filename##*.}"
echo $extension

3 C++[ | ]

#include <iostream>
#include <filesystem>
using namespace std;
int main() {
    string extension = std::filesystem::path("C:\\mydir.old\\myfile.ext").extension();
    cout << extension  << endl; // '.ext'
}

4 C#[ | ]

using System;
using System.IO;
using static System.IO.File;
class Program {
    static void Main() {
        string fileName = @"C:\mydir.old\myfile.ext";
        string extension = Path.GetExtension(fileName);
        Console.WriteLine( extension ); // .ext
    }
}

5 Go[ | ]

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println(filepath.Ext("./hello.jpg"))               // .jpg
	fmt.Println(filepath.Ext("/tmp/hello.tar.gz"))         // .gz
	fmt.Println(filepath.Ext("C:\\mydir.old\\myfile.ext")) // .ext
}

6 PHP[ | ]

$extension = pathinfo($filename, PATHINFO_EXTENSION);
$path_parts = pathinfo($filename);
$extension = $path_parts['extension'];
$extension = substr( strrchr($filename, "."), 1 );
$extension = array_pop( explode('.', $filename) );

7 VB[ | ]

Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim extension As String
extension = Path.GetExtension(fileName)

8 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}