kotlin 中字符串replace操作

package com.xg.myapplication

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log

class kotlinStringActivity : AppCompatActivity() {
    private val stringreplace = "Kotlin is a very good programming language"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_string)
        // Kotlin is Y very good progrYmming lYnguYge
        Log.d("kotlin 替换所有a 变为Y", stringreplace.replace("a", "Y"))
        //Kotlin is a very good programming languaY
        Log.d("kotlin 替换掉最后一个a后面所有的字段", stringreplace.replaceAfterLast("a", "Y"))
        // Kotlin is aY
        Log.d("kotlin 替换掉第一个a后面的所有字段", stringreplace.replaceAfter("a", "Y"))
        // Ya very good programming language
        Log.d("kotlin 替换掉第一个a前面的所有字段", stringreplace.replaceBefore("a", "Y"))
        // Yage
        Log.d("kotlin 替换掉最后一个a前面的所有字段", stringreplace.replaceBeforeLast("a", "Y"))
        // Kotlin is Y very good programming language
        Log.d("kotlin 替换掉第一个字段变成Y", stringreplace.replaceFirst("a", "Y"))
        // Kotlin is Y very good programming language
        Log.d("kotlin 替换掉第一个字段变成Y", stringreplace.replaceIndent("a1111"))
        //  K222222n is a very good programming language
        Log.d("kotlin 替换下标到另一个下标之间的内容", stringreplace.replaceRange(1, 5, "222222"))
        /**
         *
        fun String.replaceIndentByMargin(
        newIndent: String = "",
        marginPrefix: String = "|"
        ): String
        通过marginPrefix检测缩进,因为它修剪trimMargin并将其替换为newIndent。
         */
        Log.d("kotlin 替换下标到另一个下标之间的内容", stringreplace.replaceIndentByMargin("", "|"))
    }
}

 

你可能感兴趣的:(Kotlin)