---
schema_version: "1"
id: d2672dc121e5f7b796ba7fc9f90420792ab185b7efe589bff0527328050b04c3
title: Write Cleaner Code by Using Multiline Strings
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/release-notes/262-0-0/rn_apex_multiline_string.md
canonical_url: https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_multiline_string.htm&release=262.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 81912bace24f3b86ad2b3f68e870648b4e5e99301aaace126db25e908b1e4109
description: Declare Apex strings that span multiple lines without using repeated string concatenation. To interpolate values into regular and multiline strings, use the new String.template() instance method instead of the existing String.format() static method. These features make your Apex code easier to write, read, and maintain, especially when you construct HTTP payloads and other large text blocks.
products:
  - Cross Cloud
release: "262"
---
# Write Cleaner Code by Using Multiline Strings

Declare Apex strings that span multiple lines without using repeated string concatenation. To interpolate values into regular and multiline strings, use the new `String.template()` instance method instead of the existing `String.format()` static method. These features make your Apex code easier to write, read, and maintain, especially when you construct HTTP payloads and other large text blocks.

**Where:** This feature is available in all API versions. This feature applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.

**Why:** Unlike `String.format()`, which relies on index-based placeholders, `String.template()` uses named variable placeholders with the `${variableName}` syntax. You can map values directly to descriptive keys, which eliminates the need to track array indices and makes your code easier to audit and maintain.

**How:** To create a multiline string, surround a text block with three single quotes (`'''`). After the opening quotes, start the main text on a new line. Place the closing quotes either directly after the main text or on a new line.

```java
String newCustomerJson = '''
{
    "Name" : "John Doe",
    "Type" : "New Customer",
    "Interests": 
        [
            {"id": "101", "sportName: "biking"},
            {"id": "102", "sportName: "running"},
            {"id": "103", "sportName: "football"}
        ]
}''';
```

To perform string interpolation, declare a string with named variables in the format `${variableName}`. Then call `String.template(Map<String, Object> valueMap)` on the string. Pass a map where the keys are the variables defined in the string, and the values are the corresponding substitutions for those variables.

```java
String formatted = '''
{
    "Account": "${accountName}",
    "Last Updated": "${date}"
}'''.template(new Map<String, Object> {
            'accountName' => 'My Account',
            'date' => DateTime.newInstance(2018, 11, 15)
});
Assert.areEqual('''
{
    "Account": "My Account",
    "Last Updated": "2018-11-15 08:00:00"
}''', formatted);
```

#### See Also

- [_IdeaExchange_: Support multi-line String declaration in Apex](https://ideas.salesforce.com/s/idea/a0BHp000016LlqnMAC/support-multiline-string-declaration-in-apex)
- [_IdeaExchange_: String Interpolation for Apex](https://ideas.salesforce.com/s/idea/a0B8W00000GdbtjUAB/string-interpolation-for-apex)
- [_Apex Developer Guide_: Primitive Data Types](https://developer.salesforce.com/docs/atlas.en-us.262.0.apexcode.meta/apexcode/langCon_apex_primitives.htm)
- [_Apex Reference Guide_: String Methods](https://developer.salesforce.com/docs/atlas.en-us.262.0.apexref.meta/apexref/apex_methods_system_string.htm#apex_System_String_methods)