Is there a standard way to get the extension of a File
in Kotlin?
File("a/b/file.txt")
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.
A KT file contains source code written in Kotlin, a statically-typed programming language developed by JetBrains.
To get extension, we can use the following java code: int dotposition= file. lastIndexOf("."); filename_Without_Ext = file.
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('.', "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With