当前位置:网站首页>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
边栏推荐
- 接口测试经典面试题:Session、cookie、token有什么区别?
- rk3588使用npu进行模型转换和推理,加速AI应用落地
- Canvans:绘制饼图和玫瑰饼图
- 方舟基础物品指令代码大全
- 经验分享|低成本快节奏搭建企业知识管理系统的方法
- 超人飞来!Flutter 实现满屏的力量感动画!
- 学习与尝试 --> 事件风暴
- 解决gradle导包速度慢问题
- OpenEuler's Ways to Improve Resource Utilization 02: Effects under Typical Applications
- com.alibaba.fastjson.JSONException: default constructor not found. class
猜你喜欢

JMeter测试接口并发场景

Superman is coming!Flutter realizes full-screen power animation!

头条二面:你确定ThreadLocal真的会造成内存泄露?

亚洲首个!朱永官院士荣获2022年国际土壤科学联合会李比希奖

Float.parseFloat()的作用

iMeta | 深圳先进院戴磊组开发可同时提取共存菌株的组成和基因成分谱的菌株分析工具...

Use fontforge to modify font, keep only numbers

内网渗透之代理转发

学习笔记:线性表的顺序表示和实现(二级指针实现)

知乎高赞:如果一个程序员工作5年后还没成为大牛,是不是该考虑别的路子了?
随机推荐
JSD-2204-Session-Token-JWT-Day12
Mei cole studio OpenHarmony equipment development training notes - the first learn notes
文件上传接入阿里云OSS
关于KotlinAndroid遇到的小知识
Experience Sharing | A low-cost and fast-paced approach to building an enterprise knowledge management system
买股票安全吗 资金能取出来吗
MySQL8 免安装版安装
头脑风暴:打家劫舍2
数据解读!智能座舱“升级战”背后,本土供应链加速崛起
阿里财报中的饿了么:守正出奇
CSP-J2021 题解
From interview to autism, five rounds of interviews for byte software testing post, four hours of soul torture...
Kotlin annotations
Cesium中自定义材质material
有幸与美团大佬共同探讨单节点连接数超1.5W的问题
【翻译】用Argo CD揭开企业规模的持续交付的秘密成分
WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题
uni-app微信小程序如何渲染markdown
面试面到自闭,字节软件测试岗五轮面试,四个小时灵魂拷问...
LeetCode_2_两数相加