Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Get file's extension e.g. ".txt"

Tags:

java

io

kotlin

Is there a standard way to get the extension of a File in Kotlin?

File("a/b/file.txt")
like image 984
s1m0nw1 Avatar asked Dec 18 '17 15:12

s1m0nw1


People also ask

How do I get the extension of a String from Kotlin?

Get File Extension in Kotlin Kotlin – Get File Extension : In Kotlin, to extract or get the file extension, use File. extension property. File. extension is a String value.

What is the file extension for Kotlin files?

A KT file contains source code written in Kotlin, a statically-typed programming language developed by JetBrains.

How do I see file extensions on Android?

To get extension, we can use the following java code: int dotposition= file. lastIndexOf("."); filename_Without_Ext = file.


1 Answers

Disclaimer: This is only available on the JVM.

You can find very neat extensions on java.io classes like File in kotlin.io. In order to get the file extension, use the following:

File("a/b/file.txt").extension

This extension property is defined here:

public val File.extension: String
        get() = name.substringAfterLast('.', "")
like image 71
s1m0nw1 Avatar answered Sep 19 '22 07:09

s1m0nw1