文字列の先頭から指定した文字列を削除する
例えば、「Documents/hogehoge」という文字列から「hogehoge」を取り出したいときは、Stringクラスの拡張関数removePrefix
メソッドを使います。
このメソッドは文字列の先頭から指定した文字列を削除します。
prefix.kt
val str = "Documents/hogehoge"
val newStr = str.removePrefix("Documents/") // "hogehoge"
これで「hogehoge」が取り出せます。
文字列の末尾から指定した文字列を削除する
例えば、「hogehoge.csv」という文字列から拡張子を取り除いた「hogehoge」を取り出したいときは、Stringクラスの拡張関数removeSuffix
メソッドを使います。
このメソッドは文字列の末尾から指定した文字列を削除します。
suffix.kt
val str = "hogehoge.csv"
val newStr = str.removeSuffix(".csv") // "hogehoge"
これで「hogehoge」が取り出せます。
参考
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/remove-prefix.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/remove-suffix.html