In Kotlin, a raw string is defined using triple quotes ("""
). Unlike regular strings, which require escaping special characters like backslashes (\
), newlines (\n
), and tabs (\t
), raw strings preserve the exact text within the quotes.
// Regular string with escaping
val regularString = "Hello\nWorld"
println(regularString)
// Raw string without escaping
val rawString = """Hello
World"""
println(rawString)
In the above code, the regular string requires the \n
escape sequence to represent a newline. In contrast, the raw string simply includes the actual newline character in the text.
Kotlin raw strings support string interpolation, which allows you to embed expressions inside the string. You can use the $
symbol followed by the variable or expression you want to include.
val name = "John"
val message = """Hello, $name!
Welcome to Kotlin."""
println(message)
In this example, the value of the name
variable is interpolated into the raw string.
One of the most common use cases for raw strings is dealing with multi - line text, such as SQL queries, XML snippets, or multi - line comments.
val sqlQuery = """
SELECT *
FROM users
WHERE age > 18
"""
println(sqlQuery)
This makes the code more readable and easier to maintain compared to using regular strings with multiple escape sequences.
Regular expressions often contain a lot of special characters. Using raw strings can simplify the process of writing and reading regular expressions.
import kotlin.text.Regex
val regex = Regex("""\d{3}-\d{2}-\d{4}""")
val input = "123-45-6789"
if (regex.matches(input)) {
println("Valid SSN format")
}
Here, the raw string makes the regular expression more readable by eliminating the need for double - escaping the backslashes.
When using multi - line raw strings, you may want to indent the text for better code readability. Kotlin provides the trimMargin()
function to remove the leading whitespace up to a specified margin prefix.
val multiLine = """
|This is a multi - line
|raw string.
|It is more readable with indentation.
""".trimMargin()
println(multiLine)
By default, the margin prefix is |
, but you can specify a different one if needed.
When using string interpolation in raw strings, be cautious about potential security vulnerabilities, such as SQL injection or cross - site scripting (XSS). Always sanitize user - inputted data before interpolating it into a raw string.
// Example of potential SQL injection
val userInput = "1; DROP TABLE users; --"
val unsafeQuery = """SELECT * FROM users WHERE id = $userInput"""
println(unsafeQuery)
// Safe approach: use prepared statements
// (This is a high - level concept, actual implementation may vary)
Kotlin raw strings are a valuable addition to the language, offering a cleaner and more readable way to handle strings with special characters and multi - line text. By understanding the core concepts, typical usage scenarios, and best practices, intermediate - to - advanced software engineers can leverage this feature to write more efficient and maintainable code. However, it’s important to be aware of security implications when using string interpolation in raw strings.