> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin String

A `String` in Kotlin represents an immutable sequence of UTF-16 characters. On the JVM, Kotlin's `String` class is mapped directly to `java.lang.String`. Because strings are strictly immutable, any operation that appears to modify a string (such as concatenation or replacement) allocates and returns a new `String` instance rather than mutating the original object in memory.

Kotlin provides two primary types of string literals: escaped strings and raw strings.

**Escaped Strings**
Enclosed in double quotes (`"`), escaped strings support standard C-style character escaping mechanisms (e.g., `\n`, `\t`, `\"`, `\\`).

```kotlin theme={"dark"}
val escapedString: String = "First line\nSecond line"
```

**Raw Strings**
Enclosed in triple quotes (`"""`), raw strings do not support character escaping. They can span multiple lines and contain arbitrary text, including unescaped double quotes. They are typically paired with standard library functions like `trimIndent()` or `trimMargin()` to strip leading whitespace for code readability.

```kotlin theme={"dark"}
val rawString: String = """
    |SELECT * FROM users
    |WHERE age > 18
""".trimMargin()
```

**String Templates (Interpolation)**
Kotlin evaluates embedded expressions within both escaped and raw string literals using the `$` character.

* Simple variable references use a bare `$`.
* Complex expressions or property accesses require curly braces `${}`.

```kotlin theme={"dark"}
val count = 5
val simpleTemplate = "Total items: $count"
val expressionTemplate = "Doubled: ${count * 2}"
val propertyAccess = "Length: ${simpleTemplate.length}"
```

**Properties and Indexing**
`String` implements the `CharSequence` interface. Individual characters can be accessed in constant time via the indexing operator `[]`, which internally invokes the `get(index: Int)` function.

```kotlin theme={"dark"}
val text = "Kotlin"
val firstChar: Char = text[0]       // 'K'
val lastChar: Char = text.last()    // 'n'
val totalLength: Int = text.length  // 6
```

**Iteration**
Because `String` is a sequence of `Char` elements, it supports direct iteration via standard loops or higher-order functions.

```kotlin theme={"dark"}
for (char in "Kotlin") {
    // Iterates through 'K', 'o', 't', 'l', 'i', 'n'
}
```

**Equality**
Kotlin enforces a strict distinction between structural and referential equality, which applies directly to strings:

* **Structural Equality (`==`):** Evaluates whether the character sequences of two strings are identical. It safely handles nulls and compiles down to `java.lang.String.equals()` on the JVM.
* **Referential Equality (`===`):** Evaluates whether two references point to the exact same object in memory. Due to JVM string pooling, identical string literals often share the same reference.

```kotlin theme={"dark"}
val str1 = "Kotlin"
val str2 = "Kot" + "lin"
val str3 = String(charArrayOf('K', 'o', 't', 'l', 'i', 'n'))

val isStructurallyEqual = (str1 == str2)   // true
val isReferentiallyEqual = (str1 === str3) // false (different memory allocations)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
