This is the 30th day of my participation in the Wenwen Challenge

I have seen many interesting topics about Kotlin on portal.kotlin-academy.com/#/ recently. Personally, I think it is very suitable for Kotlin lovers, interested partners can consult by themselves. The interesting Kotlin series records their understanding of each question.

0 x02: Indent trimming

fun main(args: Array<String>) {
	val world = "multiline world"
	println("""
    	Hello
    	\$world"" ".trimIndent())
}
Copy the code

What is the result of the above code? Optional:

  1. Hello $world
  2.    Hello    $world
  3. Hello \multiline world
  4. doesn’t compile

Think about it and write down the answer in your mind.

Analysis of the

  1. The Raw String in Kotlin is used"" "Three double quotation marks wrapped;
  2. The Raw String in Kotlin contains non-escape characters, newlines, and any other characters. In other words, what they’re doing\Escape characters are treated as normal characters;
  3. $Symbol for processing string template expressions;
  4. trimIndent()The generic minimum indentation of all input lines is detected and removed from each line.

After the above analysis, the escape character fails, the string template replaces the world variable, and combined with the trimIndent function, the indent is removed. The answer is clearly option 3: Hello \ Multiline world

conclusion

Escape string

Most of the strings we use in Kotlin are transfer strings wrapped in “single double quotes. Escape characters are supported, and string template expressions are also supported. The result of an expression run is concatenated to the position of the expression in the string. Such as:

val love = "OpenCV \nor Android"
println("I Love $love")
println("I Love \$love")
Copy the code

The result is:

I Love OpenCV 
or Android
I Love $love
Copy the code

Raw string

In Kotlin, the content wrapped in “”” is a Raw String, which does not support escape characters and supports String template expressions. Such as:

val love = "OpenCV \nor Android"
println("""I Love $love"" ")
println("""I Love \$love"" ")
Copy the code

The running results are as follows:

I Love OpenCV 
or Android
I Love \OpenCV 
or Android
Copy the code

Trim (), trimIndent(), trimMargin()

val spaceTrim = """ Yes No Other """.trim()
println(spaceTrim)

val spaceTrimMargin = """ Yes No Other """.trimMargin()
println(spaceTrimMargin)

val spaceTrimIndent = """ Yes No Other """.trimIndent()
println(spaceTrimIndent)
Copy the code

The running results are as follows:trimMargin()Take a look alone, the source code is as follows:

public fun String.trimMargin(marginPrefix: String = "|"): String =
    replaceIndentByMargin("", marginPrefix)
Copy the code

Parameter for | marginPrefix default parameters. Use it as a boundary character to facilitate alignment of raw strings. Take a look at the following examples:

val marginOne = """ |Yes |No |Other """.trimMargin()
println(marginOne)

val marginTwo = """ >Yes >No >Other """.trimMargin()
println(marginTwo)

val marginThree = """ >Yes >No >Other """.trimMargin(">")
println(marginThree)
Copy the code

The running results are as follows:

Yes
No
Other
        >Yes
            >No
        >Other
Yes
No
Other
Copy the code