Strings
Auto-generated from
src/stdlib/strings.mcrs— do not edit manually.
API
str_len v1.0.0
Returns the length of the string stored at rs:strings.{s}.
LIMITATION (MC 1.21.4): execute store result score ... run data get storage returns 1 for any plain string NBT tag, not the character count. To get a true character count, the string must be stored as an NBT list of single-char strings (e.g. ["h","e","l","l","o"]), in which case data_get returns the list length correctly.
For literal strings whose length is known at compile time, prefer using the constant directly — the compiler will fold it.
fn str_len(s: string) -> intParameters
| Parameter | Description |
|---|---|
s | NBT field name in rs:strings (e.g. "A") |
Returns: 1 for plain string tags; list element count for char-list strings.
Example
str_len("A") // result: 1 (plain string) or char count (char-list)str_concat v1.0.0
Concatenates two strings from rs:strings and writes the result to rs:strings.Result as an NBT list [a_value, b_value].
The result list is suitable for use in tellraw JSON text components: tellraw @a
LIMITATION (MC 1.21.4): MC has no native string concatenation command. Single-string concatenation (e.g. "ab" from "a"+"b") requires a recursive macro helper function; this is out of scope for a pure stdlib. The NBT list representation works for display (tellraw) but cannot be passed back to further string operations without conversion.
ALTERNATIVE for display: use a tellraw JSON array directly: tellraw @a [{"storage":"rs:strings","nbt":"A"},{"storage":"rs:strings","nbt":"B"}]
fn str_concat(a: string, b: string)Parameters
| Parameter | Description |
|---|---|
a | NBT field name of the first string in rs:strings (e.g. "A") |
b | NBT field name of the second string in rs:strings (e.g. "B") |
Returns: rs:strings.Result is set to an NBT list [valueA, valueB]
Example
str_concat("A", "B") // rs:strings.Result = ["hello", "world"]str_contains v1.0.0
Checks whether the string at rs:strings.{s} contains the substring at rs:strings.{sub}. Returns 1 if found, 0 if not found.
NOT FEASIBLE (MC 1.21.4): Minecraft datapacks do not provide a native substring search command. There is no execute if data form that tests whether one string is contained within another.
THIS FUNCTION ALWAYS RETURNS 0.
WORKAROUNDS for datapack authors:
- If sub is a compile-time constant and the check position is known, use str_slice to extract a window and compare via scoreboard.
- Pre-process strings server-side and store a presence flag in NBT.
- Use a macro function (MC 1.20.2+) that iterates over character positions with
$(var)substitution and returns early on match.
fn str_contains(s: string, sub: string) -> intParameters
| Parameter | Description |
|---|---|
s | NBT field name of the haystack string in rs:strings |
sub | NBT field name of the needle string in rs:strings |
Returns: 0 (always — not implemented due to MC limitation)
str_slice v2.2.0
Extracts a substring from rs:strings.{s} and writes it to rs:strings.Result. Uses the MC 1.20.2+ data modify ... set string ... start end feature.
Indices follow Java/Python slicing semantics:
- start is inclusive, 0-based.
- end is exclusive (end = start + length).
- Negative end counts from the end of the string (e.g. -1 = all but last char).
- Omitting end (use a very large positive int like 999999) copies to end of string.
Examples: str_slice("A", 0, 5) → first 5 characters of rs:strings.A str_slice("A", 2, -1) → all but first 2 and last 1 characters
fn str_slice(s: string, start: int, end: int)Parameters
| Parameter | Description |
|---|---|
s | NBT field name of the source string in rs:strings (e.g. "A") |
start | Start index (inclusive, 0-based) |
end | End index (exclusive); negative counts from string end |
Returns: rs:strings.Result contains the extracted substring
Example
str_slice("A", 0, 5) // first 5 chars of rs:strings.A
str_slice("A", 2, -1) // drop first 2 and last 1 chars