Kotlin learning - string manipulation

string template

1. Like many scripting languages, Kotlin allows us to refer to local variables in string literals, just add a front $.

fun printName(name:String) {
    
    
    println("My name is $name")
}

Call code:

fun main() {
    
    
    printName("Lily")
}

//运行结果
My name is Lily

2. You can also quote more complex expressions, just enclose the expressions in parentheses:${表达式}

fun printName(name: String) {
    
    
   println("Hello , ${
      
      if (name.isNotEmpty()) name else "someone"}")
}

Call code:

fun main() {
    
    
    printName("Lily")
}

//运行结果
Hello , Lily

string operator substring

Use the string truncation operator substringto truncate a segment of the name. The intercepted string contains the characters of the start index, but does not contain the characters of the end index.

const val NAME = "Lucky's friends"

fun main() {
    
    
	val nameEndIndex = NAME.indexOf("\'")
	val name = NAME.substring(0, nameIndex)
	println("nameEndIndex: $nameEndIndex | name: $name")
}

//运行结果:nameEndIndex: 5 | name: Lucky

You can also use untilkeywords to achieve the same result as above.

const val NAME = "Lucky's friends"

fun main() {
    
    
	val nameEndIndex = NAME.indexOf("\'")
	val name = NAME.substring(0 until nameEndIndex)
	println("nameEndIndex: $nameEndIndex | name: $name")
}

string operator split

const val NAMES = "Jack,Lucky,Tom"

fun main() {
    
    
	val names = NAMES.split(",")
    println(names[2])
}
//运行结果:Tom

splitReturns a List collection. The List collection supports the destructuring syntax feature , which allows multiple variables to be assigned in one expression. Deconstruction is often used to simplify the assignment of variables. Let's see an example:

const val NAMES = "Jack,Lucky,Tom"

fun main() {
    
    
	//解构语法特性
    val (s1, s2, s3) = NAMES.split(",")
    println("$s1,$s2,$s3")
}

//运行结果:Jack,Lucky,Tom

Of course, there is a little trick. If you don’t want one of the elements, just replace the variable with an underscore. For example, if we don’t want the second name, modify the code as follows:

const val NAMES = "Jack,Lucky,Tom"

fun main() {
    
    
    val (a, _, b) = NAMES.split(",")
    println("$a,$b")
}

//运行结果:Jack,Tom

string operator replace

The replacement operator, the following is a simple use, you can see that a new string is generated after the replacement, and the original string has not changed.

fun main() {
    
    
	val originStr = "reining day"
	val newStr1 = originStr.replace("e", "a")
	println(newStr1)
	println(originStr)
}

//运行结果:
//raining day
//reining day

Let's look at a more complex example, you can pass in a regular expression for replacement, see the following example:

fun main() {
    
    
	//加密下面的字符串
	val fruits = "apple,banana,pear"
	//第一个是正则表达式,决定替换哪些字符
    //第二个是匿名函数,决定改如何替换正则表达式中的字符
    val encryptionFruits = fruits.replace(Regex("[a,b,p]")) {
    
    
    	when (it.value) {
    
    
            "a" -> "6"
            "b" -> "7"
            "p" -> "8"
            else -> it.value
        }
    }
    println(encryptionFruits)
}
//运行结果:688le,76n6n6,8e6r

String operator forEach

const val NAME = "Lucky's friends"

fun main() {
    
    
	//遍历forEach
    NAME.forEach {
    
    
        print("$it ** ")
    }
}

//运行结果:L ** u ** c ** k ** y ** ' ** s **   ** f ** r ** i ** e ** n ** d ** s ** 

String operators == and ===

In Kotlin , it is ==equivalent to Java equals, which compares the contents of strings, returns the same trueand returns differently false.

In Kotlin , it is ===equivalent to Java ==, which compares the addresses of strings, returns the same trueand returns differently false.

In the following example, two string constants are declared. The first string declared will be stored in the string constant pool. When declaring, it will name2first go to the constant pool to see if there is any. If it is defined before, it directly points to the object, so name1and name2both point to the same string in the constant pool. Both the address and the content are the same.

fun main() {
    
    
    val name1 = "Lucky"
    val name2 = "Lucky"
    println(name1 == name2)
    println(name1 === name2)
}

//运行结果:true和true

Let's look at another example:

fun main() {
    
    
   val name1 = "Lucky"
   val name3 = "lucky".capitalize()
   println(name1 == name3)
   println(name1 === name3)
}
//运行结果:true和false

name3Although it name1is the same as the content, capitalize()a new object will be generated when it is called again, so the address is the same with the same content.

Guess you like

Origin blog.csdn.net/kongqwesd12/article/details/130967892