@文心快码kotlin string indexof 文心快码 在Kotlin中,indexOf函数是一个非常重要的字符串处理方法,用于查找子字符串或字符在字符串中首次出现的索引位置。以下是对indexOf函数的详细解释和示例: indexOf函数的作用: indexOf函数用于查找一个子字符串或字符在字符串中首次出现的索引位置。 基本使用示例: 查找子...
基本类型 Int、Long、Float、Double、Boolean、Char、String 类型之间的转换 toInt()、toLong()、toFloat()、toDouble()、toBoolean()、toChar()、toString() 2、数组 (1)、数组的声明 (2)、数组元素操作 3、字符串 (1)、字符串操作 查找——indexOf() 截取——substring() 替换——replace()...
drop(n: Int): String 去掉前n个字符,返回其余的字符串,等同于substring(n) //删掉前3个字符 println(str.drop(3)) //输出结果:456789 1. 2. 3. dropLast(n: Int): String 去掉后n个字符,返回其余的字符串,等同于substring(0, str.length – n) //删掉后4个字符 println(str.dropLast(4)) //...
val origin:String=tv_origin.text.toString()varorigin_trim:String=originif(origin_trim.indexOf('.')>0){origin_trim=origin_trim.substring(0,origin_trim.indexOf('.'))} 在这些字符串处理方法里面,唯一的区别是split方法的返回值,在Java中,split方法返回的是String数组,即String[];但在Kotlin中,split方...
val origin:String = tv_origin.text.toString() var origin_trim:String = origin if (origin_trim.indexOf('.') > 0) { origin_trim = origin_trim.substring(0, origin_trim.indexOf('.')) } 在这些字符串处理方法里面,唯一的区别是split方法的返回值,在Java中,split方法返回的是String数组,即String...
/** * Returns the length of this char sequence. */ @kotlin.internal.InlineOnly public inline fun CharSequence.count(): Int { return length } 获取指定位置的字符 val string = "Kotlin" >> println(string[2]) ① >> println(string.elementAt(2)) ② ...
// 首先通过indexOf判断是否存在要被替换的子字符串 // do while循环添加被替换之后的字符串,因为字符串有可能是有多个地方需要替换,所有通过occurrenceIndex判断是否还有需要被替换的部分 public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { run { var...
即StringIndexOutOfBoundsException异常 2.2、用subSequence()函数截取 在Kotlin中除了使用上面讲解到的使用subString()截取字符串外,还可以使用subSequence()函数截取。 我们看一看其源码实现: public fun subSequence(startIndex: Int, endIndex: Int): CharSequence public fun CharSequence.subSequence(range: IntRange)...
即StringIndexOutOfBoundsException异常 2.2、用subSequence()函数截取 在Kotlin中除了使用上面讲解到的使用subString()截取字符串外,还可以使用subSequence()函数截取。 我们看一看其源码实现: public fun subSequence(startIndex: Int, endIndex: Int): CharSequencepublic fun CharSequence.subSequence(range: IntRange):...
class TestKotlin { fun function() { var mutableMap: MutableMap<Int, String> = mutableMapOf(1 to "one", 2 to "two", 3 to "three") print(mutableMap[1]) } } Set创建以及遍历 与List创建差不多,Set的创建与遍历、过滤如下 代码语言:javascript 代码运行次数:0 运行 AI代码解释 class TestKotl...