Menu

GDScript Strings: Split, Replace, Substr and Join

Working with text in GDScript: quotes and escapes, joining strings, length and indexing, searching, replacing, splitting and joining, trimming, case and comparing.

This page includes runnable editors. Edit, run, and see output instantly.

A String holds text. Write it in double or single quotes:

Hello, Ana!
11

+ joins strings, but only strings. "Level " + 3 is an error. Convert numbers with str(): "Level " + str(3). For several values, string formatting is cleaner.

Escapes and Multiline Strings

She said "run"
Name:	Ana
one
two
A triple-quoted string
keeps its line breaks.

\n is a new line, \t a tab and \" a quote inside double quotes. Triple quotes keep the line breaks you type.

Indexing and Substrings

G e
Engine
Godot
God ine

Indexes start at 0, and a negative index counts from the end. word[0] = "J" replaces one character in that variable only: a string is a value, not a shared object. Every method below returns a new string and leaves the original as it was.

Searching

true
false
5
20
2
true true

Replacing, Splitting and Joining

["sword", "shield", "potion"]
3 items, the second is shield
sword | shield | potion
sword, shield, potion

split() returns a PackedStringArray. Pass false as the second argument to drop empty parts: "a,,b".split(",", false) gives ["a", "b"].

Trimming, Case and Padding

[Ana]
GAME OVER
game over
Player Name
007
ababab

strip_edges() removes spaces, tabs and line breaks from both ends. Use it on text a player typed before you compare it.

Comparing Strings

== compares exactly, so case matters. For a case-insensitive check, compare lowercase copies or use nocasecmp_to(), which returns 0 when the strings match:

false
true
true
true

Looping Over Characters

A for loop over a string gives one character at a time:

4 vowels

Frequently Asked Questions

How do I join strings in GDScript?

Use + between strings: "Hello " + player_name. Numbers must be converted first with str(), as in "Level " + str(3). For several values, % formatting is shorter: "%s is level %d" % [player_name, level].

How do I check if a string contains text in Godot?

Use in or contains(): "God" in title and title.contains("God") both return true or false. find() gives the position of the first match, or -1.

How do I split a string in GDScript?

Call split() with the separator: "a,b,c".split(",") returns ["a", "b", "c"] as a PackedStringArray. The reverse is ",".join(parts).

Do string methods change the original string in GDScript?

No. Methods like replace() and to_upper() return a new string and leave the original unchanged, so assign the result back: text = text.replace("a", "b"). Strings are values, not shared objects: changing one character with text[0] = "J" changes only that variable.

Coddy programming languages illustration

Learn to code with Coddy

Get started