> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/non-primitive-reference-types/string/multiline-string.md).

# Multiline String

## 1. Using Text Blocks (Java 15 and above)

```java
String s = """
           text
           text
           text
           """;
```

## 2. Using + Operator

```java
String s = "Some text 1,\n"
         + "Some text 2,\n"
         + "Some text 3,\n"
         + "Some text 4,\n"
         + "Some text 5,\n"
         + "Some text 6";
```

## 3. Using StringBuilder

```java
String s = new StringBuilder()
           .append("Some text 1,\n")
           .append("Some text 2,\n")
           .append("Some text 3,\n")
           .append("Some text 5,\n")
           .append("Some text 6,\n")
           .append("Some text 7")
           .toString();
```

## 4. Using String.format()

```java
String s = String.format("%s\n%s\n%s\n%s\n%s\n%s"
         , "Some text 1,"
         , "Some text 2,"
         , "Some text 3,"
         , "Some text 4,"
         , "Some text 5,"
         , "Some text 6"
);
```

## 5. Using String.join()

```java
// Joining multiple strings
String s = String.join("\n"
         , "Some text 1,"
         , "Some text 2,"
         , "Some text 3,"
         , "Some text 4,"
         , "Some text 5,"
         , "Some text 6"
);

// Joining a list of strings with a comma
List<String> names = List.of("Alice", "Bob", "Charlie");
String result = String.join(", ", names);
System.out.println(result); // Alice, Bob, Charlie

// Joining array elements with a pipe (|)
String[] tokens = {"token1", "token2", "token3"};
String result = String.join(" | ", tokens);
System.out.println(result); // token1 | token2 | token3
```

## 6. Using String.concat()

```java
String s= "This is a\n"
           .concat("multiline string\n")
           .concat("using String.concat().");
```

## Comparison

<table data-full-width="true"><thead><tr><th width="201">Method</th><th width="240">Scenario</th><th>Use Case</th></tr></thead><tbody><tr><td><strong>Text Blocks (Java 15+)</strong></td><td>Readability, ease of use, avoiding escape characters</td><td>Defining long, static, multiline strings such as SQL queries, HTML content, JSON, or configuration settings. Ideal for one-time definitions.</td></tr><tr><td><strong>+ Operator</strong></td><td>Simple concatenation, small multiline strings</td><td>Quick concatenation in simple cases, such as building a small multiline string inside a method or loop for logging purposes.</td></tr><tr><td><strong>StringBuilder</strong></td><td>Performance, especially in loops and iterations</td><td>Building multiline strings in loops, iterative concatenation where performance is critical. Use for dynamically constructing strings in iterative processes.</td></tr><tr><td><strong>String.format()</strong></td><td>Formatting with variable substitution</td><td>Creating formatted strings with variables, especially when readability is important. Useful for constructing strings with embedded variables.</td></tr><tr><td><strong>String.join()</strong></td><td>Joining multiple lines from a collection</td><td>Joining multiple strings from a collection or array, often used in cases where you have a list of strings that need to be joined with a delimiter.</td></tr><tr><td><strong>String.concat()</strong></td><td>Chaining method calls, small multiline strings</td><td>Simple chaining of strings when constructing small multiline strings in method chains. Suitable for cases where the number of lines is small and readability is not a major concern.</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/non-primitive-reference-types/string/multiline-string.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
