Kotlin commonPrefixWith用法及代码示例

本文方法及代码示例基于Kotlin 2.1.20 Released

commonPrefixWith 所在包 kotlin.text.commonPrefixWith,其相关用法介绍如下:

用法:

kotlin 复制代码
fun CharSequence.commonPrefixWith(
    other: CharSequence, 
    ignoreCase: Boolean = false
): String

返回最长的字符串 prefix 使得这个 char 序列和 other char 序列都以此前缀开头,注意不要拆分代理对。如果 this 和 other 没有公共前缀,则返回空字符串。

代码示例:

kotlin 复制代码
import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
    //sampleStart
    println("Hot_Coffee".commonPrefixWith("Hot_cocoa")) // Hot_
    println("Hot_Coffee".commonPrefixWith("Hot_cocoa", true)) // Hot_Co
    println("Hot_Coffee".commonPrefixWith("Iced_Coffee")) //
    //sampleEnd
}

// 输出
Hot_
Hot_Co

参数:ignoreCase-true 匹配字符时忽略字符大小写。默认false.

相关方法

相关推荐
Tiger_Hu8 分钟前
Android系统日历探索
android
whysqwhw9 分钟前
RecyclerView卡顿
android
whysqwhw13 分钟前
RecyclerView 与 ListView 在性能优化方面
android
檀越剑指大厂3 小时前
容器化 Android 开发效率:cpolar 内网穿透服务优化远程协作流程
android
MiyamuraMiyako4 小时前
从 0 到发布:Gradle 插件双平台(MavenCentral + Plugin Portal)发布记录与避坑
android
NRatel5 小时前
Unity 游戏提升 Android TargetVersion 相关记录
android·游戏·unity·提升版本
叽哥7 小时前
Kotlin学习第 1 课:Kotlin 入门准备:搭建学习环境与认知基础
android·java·kotlin
风往哪边走7 小时前
创建自定义语音录制View
android·前端
用户2018792831677 小时前
事件分发之“官僚主义”?或“绕圈”的艺术
android
用户2018792831677 小时前
Android事件分发为何喜欢“兜圈子”?不做个“敞亮人”!
android