> For the complete documentation index, see [llms.txt](https://docs.rewst.help/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rewst.help/documentation/jinja/common-jinja-examples/splitting-strings-replacing-characters-and-combining-strings.md).

# Split strings, replace characters, and combine strings

Mastering string manipulation techniques in Jinja templating is essential for creating dynamic and well-formatted templates. Incorporate these techniques into your Jinja templates to optimize your data processing capabilities and improve their readability. There are three essential string operations:

1. Splitting strings
2. Replacing characters
3. Combining strings

### **Split strings: Use .split()**

The `.split()` method in Jinja is employed to divide a string into multiple substrings based on a specified character or a set of characters. By providing a delimiter, you can split the string at occurrences of that character.

```django
{%- set mystring = "this_is_a_string" -%}
{{- 
    [
        item for item in mystring.split("_")
    ] 
-}}
```

In this example, the string `"this_is_a_string"` is split at each underscore (`_`), resulting in the output: `["this", "is", "a", "string"]`.

### **Replace characters: Use .replace() and | regex\_replace()**

The `.replace()` method or the `| regex_replace()` filter is utilized to replace specific characters in a string with another set of characters.

```django
{%- set mystring = "this_is_a_string" -%}
{{-mystring.replace("_", " ")-}}
{#- another way is like this -#}
{{- mystring | regex_replace("_", " ") -}}
```

In both cases, the underscore (`_`) in the string `"this_is_a_string"` is replaced with a space, resulting in the output: `"this is a string"`.

### **Combine strings: Use concatenation**

Combining strings in Jinja is achieved similarly to Python. Strings can be concatenated using either the `~` or `+` operators.

```django
{%- set string1 = "hello" -%}
{%- set string2 = "world" -%}
{{ string1~string2 }}
```

```django
{%- set string1 = "hello" -%}
{%- set string2 = "world" -%}
{{ string1 + string2 }}
```

In this example, the strings `"hello"` and `"world"` are concatenated to form the output: `"helloworld"`.

Note that in Jinja, the `~` operator is the preferred operator for this function.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rewst.help/documentation/jinja/common-jinja-examples/splitting-strings-replacing-characters-and-combining-strings.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
