Menu
CoddyTech

String Operations

Lesson 19 of 48 in Coddy's GDScript for Luau Developers course.

Luau strings are immutable byte sequences; .. concatenates them. GDScript String has text value semantics and uses + for concatenation. In both examples a replacement creates new text rather than mutating the original binding. Luau string.gsub uses patterns and also returns a replacement count; GDScript replace performs literal replacement and returns the new string. Do not port a pattern as though it were a literal delimiter. Choose normalization rules explicitly rather than silently accepting malformed input after trimming.

Complete Luau comparison. Save the files together and run main.luau with the Luau CLI:

File main.luau:

local s = "a-b-a"
local t, count = string.gsub(s, "-", "/")
print(s .. " " .. t)
print(count)

Luau output:

a-b-a a/b/a
2

Complete GDScript comparison:

File main.gd:

extends Node

func _ready() -> void:
	var s: String = "a-b-a"
	var t: String = s.replace("-","/")
	print(s," ",t)

GDScript output:

a-b-a a/b/a
challenge icon

Challenge

Easy

Inputs text and prefix. Strip surrounding whitespace from text, then remove one matching prefix. Preserve interior spaces.

Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.

Your code goes in exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.

Try it yourself

extends Node

func contract_ok(data: Array) -> bool:

	return true

func _ready() -> void:
	var data: Array = JSON.parse_string(OS.read_string_from_stdin())
	if not contract_ok(data):
		print("API_CONTRACT_FAILED")
		return
	print("result=" + Exercise.solve(data))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in GDScript for Luau Developers

Practice on your own: Online GDScript compiler