当前位置:网站首页>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
边栏推荐
猜你喜欢
NAACL2022 NER SOTA - RICON study notes
fillder4 keeps prompting the system proxy was changed, watch me solve it
iMeta | 深圳先进院戴磊组开发可同时提取共存菌株的组成和基因成分谱的菌株分析工具...
音视频技术开发周刊 | 257
Redis布隆过滤器
高数_复习_第3章:一元函数积分学
Questions about Mac terminal custom commands and Mysql command
什么是仿射函数?
How can recommender systems be trusted?A review of the latest "Trusted Recommender System" from Rutgers University, a 43-page pdf explaining the composition and technology of trusted RS
From interview to autism, five rounds of interviews for byte software testing post, four hours of soul torture...
随机推荐
Mysql management commands
Questions about Mac terminal custom commands and Mysql command
学习笔记:线性表的顺序表示和实现(二级指针实现)
PHPUnit 单元测试
XTU OJ 1075 求最小公倍数
PyTorch入门(六):模型的训练套路
Kotlin学习笔记
学习与尝试 --> 事件风暴
1088 N的阶乘
学习笔记:栈的应用1_递归(重点)
虚假信息处理最新有何进展?KDD2022《打击错误信息和应对媒体偏见》教程,161页ppt
Matlab用回归、SEIRD模型、聚类预测美国总统大选、新冠疫情对中美经济的影响
Kotlin解析String路径小知识
tar zcf是单线程瓶颈
From interview to autism, five rounds of interviews for byte software testing post, four hours of soul torture...
What are the latest developments in the handling of false information?KDD2022 "Fighting Misinformation and Responding to Media Bias" tutorial, 161 pages ppt
OneNote 教程,如何在 OneNote 中检查拼写?
Bluu海鲜公司推出首批实验室培育的鱼类产品
阿里财报中的饿了么:守正出奇
五大理由告诉你为什么开发人员选择代码质量静态分析工具Klocwork来实现软件安全