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.

相关方法

相关推荐
冬奇Lab20 小时前
OpenClaw 源码深度解析(一):Gateway——为什么需要一个"中枢"
人工智能·开源·源码阅读
不爱说话郭德纲1 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
Sinclair1 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
A0微声z1 天前
Kotlin Multiplatform (KMP) 中使用 Protobuf
kotlin
雮尘1 天前
手把手带你玩转Android gRPC:一篇搞定原理、配置与客户端开发
android·前端·grpc
ktl1 天前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton2 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab2 天前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇2 天前
AOSP15 Input专题InputManager源码分析
android·操作系统
lhDream2 天前
Kotlin 开发者必看!JetBrains 开源 LLM 框架 Koog 快速上手指南(含示例)
kotlin