当前位置:网站首页>Kotlin parsing String path knowledge
Kotlin parsing String path knowledge
2022-08-08 20:33:00 【Don't forget the original intention of z】
Parse String path, simple knowledge is stable
fun main(args: Array) {
val url = “/users/yole/kotlin-book/chapter.doc”
fun prasePath(path : String){
//The part before the last specified string is all saved
val a = path.substringBeforeLast("/")
println(a)
//The last oneAfter the specified string, it is the file name
val b = path.substringAfterLast("/")
println(b)
//String. Before is the file name
val fileName = b.substringBeforeLast(".")
//String. After that is the extension
println(fileName)
val extension = b.substringAfterLast(".")
println(extension)
}
//Create a method for parsing file paths using regular expressions
fun parsePath1(path:String){
//Create a regular expression
//(.+)/ The string before the last slash (.+)\The last .(.+) suffix name
val regex = """(.+) /(.+).(.+)""".toRegex()
println(regex)
//match
val matchResult = regex.matchEntire(path)
println(matchResult)
//Parse
if(matchResult !=null){
val (a,b,c) = matchResult.destructured
println(“a: a , n a m e a,name a,nameb,ext:$c”)
println(a)
}
}
prasePath(url)
parsePath1(url)
}
The output is as follows:
/users/yole/kotlin-book
chapter.doc
chapter
doc
The following is the printed result of the regular expression parsing
(.+)/(.+).(.+)
[email protected]
a:/users/yole/kotlin-book,namechapter,ext:doc
/users/yole/kotlin-book
边栏推荐
猜你喜欢
What are the latest developments in the handling of false information?KDD2022 "Fighting Misinformation and Responding to Media Bias" tutorial, 161 pages ppt
NAACL2022 NER SOTA - RICON study notes
学习与尝试 --> 事件风暴
深度学习初步认知
一文教你普罗米修斯Prometheus的基础应用
监控工具普罗米修斯(Prometheus)的介绍与安装
莫让“学院派”限制我们的思维:在数组的中间位置删除数据一定比链表慢?
学习笔记:第二部分 队列
瑞吉外卖项目实战Day06--手机端
接口测试经典面试题:Session、cookie、token有什么区别?
随机推荐
Mysql管理指令
VSCode 必知必会的 20 个快捷键
使用fontforge修改字体,只保留数字
Kotlin解析String路径小知识
PHP使用glob()查找文件
DCT变换
内网渗透之代理转发
解决gradle导包速度慢问题
新库上线 | CnOpenDataA股上市公司基本信息数据
C语言关键字入门 这一篇足矣
简单Swing界面笔记
Experience Sharing | A low-cost and fast-paced approach to building an enterprise knowledge management system
Kotlin基础稳固第一天
0-1 背包问题
门外汉掌握数据分析处理技术的路线图
正则表达式的限定符、或运算符、字符类、元字符、贪婪/懒惰匹配
Little knowledge about KotlinAndroid encounters
Gradle简单到使用kotlin编写到常用命令
同花顺买股票软件正规吗?安全吗?
Float.parseFloat()的作用