Raw Strings , how are these different from escaped strings and where should be use these

Viewed 10264

As per my research,

Kotlin has two types of string literals:-

Escaped strings that may have escaped characters in them .

val s = "Hello ,World\n" +
    "from escaped string\n"+
    "kotlin"

Raw string is delimited by a triple quote ("""), contains no escaping and can contain newlines and any other characters:

val m = """Hello, World
       |from raw string
       |kotlin """.trimMargin()

These strings can be used in multi lines without the need to concatenate each line and an without escaping.

Do we use raw strings only for simplicity and easy implementation or do these offer some better performance in any case?

And are these any other use-cases where we should consider using raw strings?

3 Answers

Your answer is well explained here at this website. I am going to include only essential part of it here.

String in Kotlin can be used in multiple ways as described in the above link. It is purely depends upon the requirement for which to use. If you have extra large string like html page etc then you can go with Raw string delimited by triple quote ("""). And in where you have short strings then you could use Escaped strings.

There is no real performance difference between these but depends upon how much string concatenation you are using while building values in it.

I don't know about any performance difference between the two string literal types, but there is at least one interesting use case where you should consider using raw strings: regular expressions.

Predefined character classes and many other constructs in regular expressions are introduced by a \ character (e.g. \s to match a whitespace character). When including those in a string, you need to escape them: so, for any such character in a regular expression, you need to write two, such as "\\d" to match a single digit.

However, raw strings allow you to skip the escaping part, leading to cleaner and slighlty more concise regular expressions than those you would write when your only string literal type is escaped, as it happens in Java.

Note that raw strings are not a new concept or an idea introduced by Kotlin. For example, Python has had them for a long time.

Use-case: query syntax coloring in Room queries

With Kotlin's raw strings you can write queries on multiple lines in Room's Dao, example:

@Dao
interface HappyDao {
    @Query(
            """
            SELECT
            One,
            Two,
            Three
            FROM MYTABLE
            """
    )
    fun getAll(): List<MyObject>
}

And still have the syntax coloring of the Room validator as if you are writing everything on one line (along with alerts on query syntax errors during code writing). It is especially very useful with long queries, with many fields or joins.

Without raw strings it would be like this:

@Dao
interface HappyDao {
    @Query(
            "SELECT" +
            "One," +
            "Two," +
            "Three" +
            " FROM MYTABLE"
    )
    fun getAll(): List<MyObject>
}

and it would not benefit of Room's syntax coloring.

Related