Kotlin 函数用于获取子字符串的开始和结束索引 [英] Kotlin function for getting start and end index of substring

查看:28
本文介绍了Kotlin 函数用于获取子字符串的开始和结束索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一些方法或方法组合可以获得这些结果.

I'm wondering is there some method or methods combo that could obtain these results.

我的意思是你有一个字符串 ex.Hello world,你好吗?"我想实现以下功能,以获取子字符串的开始和结束索引在那个字符串中.

What I meant is that you have a string ex. "Hello world, how are you?" And I want to achieve following functionality, to get start and end index of substring in that string.

例如.substring = "world",起始索引为 6,结束索引为 10

ex. substring = "world", and start index would be 6, and end index 10

标准的 kotlin 库中是否有类似的东西?

Is there something like this in standard kotlin libraries?

推荐答案

类似的事情?

val s = "Hi there"
val substringToFind = "there"

val start = s.indexOf(substringToFind)
val end = start + substringToFind.length

println(s.substring(start,end))

输出:那里

这篇关于Kotlin 函数用于获取子字符串的开始和结束索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆