Ben Chuanlong Du's Blog

It is never too late to learn.

String functions in Kotlin

In [1]:
val withoutIndent ="""
        ABC
        123
        456
    """.trimIndent()
withoutIndent
Out[1]:
ABC
123
456

Below sounds like a bug somewhere as it is not what trimIdent supposed to do.

In [2]:
val sql ="""
    select
        *
    from
        some_table
    where
        id = 100
    order by
        id
    """.trimIndent()
sql
Out[2]:
select
*
from
some_table
where
id = 100
order by
id

trimMargin

trimMargin is more flexible and useful than trimIndent.

In [3]:
val withoutMargin1 = """ABC
                |123
                |456""".trimMargin()
withoutMargin1
Out[3]:
ABC
123
456
In [4]:
val withoutMargin2 = """
    #XYZ
    #foo
    #bar
""".trimMargin("#")
withoutMargin2
Out[4]:
XYZ
foo
bar
In [5]:
val sql ="""
    |select
    |    *
    |from
    |    some_table
    |where
    |    id = 100
    |order by
    |    id
    """.trimMargin()
sql
Out[5]:
select
    *
from
    some_table
where
    id = 100
order by
    id
In [6]:
"".isBlank()
Out[6]:
true
In [7]:
"""
                  
""".isBlank()
Out[7]:
true
In [8]:
"a".isBlank()
Out[8]:
false

split

In [2]:
"a b c".split(" ")
Out[2]:
[a, b, c]
In [ ]:

Comments