🔠 String#
#bs.string:help
Manage and transform strings with a versatile set of manipulation and search functions.
“Words are the most powerful drug used by mankind.”
—Rudyard Kipling
🔧 Functions#
You can find below all functions available in this module.
Concat#
- #bs.string:concat
Merge multiple strings from an array into a single concatenated string.
- Inputs:
Storage
bs:in string.concat.list: List of strings to concatenate.- Outputs:
Storage
bs:out string.concat: The concatenated string.
Example: Concatenate Hello , and World then display the result:
data modify storage bs:in string.concat.list set value ["Hello ","World"]
function #bs.string:concat
tellraw @a [{"text":"The merged string is \""},{"storage":"bs:out","nbt":"string.concat"},{"text":"\""}]
Technical Limitation
If you want to use double quotes (") within a string, you must escape them with a backslash: \".
Credits: Aure31
Convert Case#
- #bs.string:upper
Convert text to uppercase.
- Inputs:
Storage
bs:in string.upper.str: The string to convert to uppercase.- Outputs:
Storage
bs:out string.upper: The uppercase string.
Example: Convert hello world to HELLO WORLD then display the result:
data modify storage bs:in string.upper.str set value "hello world"
function #bs.string:upper
tellraw @a [{"text":"The uppercase string is \""},{"storage":"bs:out","nbt":"string.upper"},{"text":"\""}]
- #bs.string:lower
Convert text to lowercase.
- Inputs:
Storage
bs:in string.lower.str: The string to convert to lowercase.- Outputs:
Storage
bs:out string.lower: The lowercase string.
Example: Convert HELLO WORLD to hello world then display the result:
data modify storage bs:in string.lower.str set value "HELLO WORLD"
function #bs.string:lower
tellraw @a [{"text":"The lowercase string is \""},{"storage":"bs:out","nbt":"string.lower"},{"text":"\""}]
Technical Limitation
These functions support most UTF-8 characters, but they do not support the double quote character ("). Attempts to use or escape double quotes will result in errors.
Credits: Aure31
Find#
- #bs.string:find
Search for a substring within a string. Find the first occurrence.
- Inputs:
Storage
bs:in string.find:Find data
str: String to search in.
substr: Substring to search for.
- Outputs:
Return: Integer index of the first occurrence or -1 if not found.
Example: Find the first occurrence of world in hello world then display the index:
data modify storage bs:in string.find set value {str:"hello world",substr:"world"}
execute store result score #c bs.ctx run function #bs.string:find
# return 6
tellraw @a [{"text":"Found at index: \""},{"score":{"name":"#c","objective":"bs.ctx"}},{"text":"\""}]
- #bs.string:find_all
Search for a substring within a string. Find all occurrences or a specific number of matches.
- Inputs:
Storage
bs:in string.find_all:Find data
str: String to search in.
substr: Substring to search for.
occurrence: Number of occurrences to find (-1 for all occurrences).
- Outputs:
Storage
bs:out string.find_all: List of indices where the substring was found, in order of occurrence.Return: Number of occurrences found.
Example: Find all occurrences of world in hello world world then display the indices:
data modify storage bs:in string.find set value {str:"hello world",substr:"world",occurrence:-1}
function #bs.string:find_all
# return [6,12]
tellraw @a [{"text":"Found at indices: \""},{"storage":"bs:out","nbt":"string.find"},{"text":"\""}]
Technical Limitation
These functions do not support the double quote character ("). Attempts to use or escape double quotes will result in errors.
Credits: Aure31
Replace#
- #bs.string:replace
Replace occurrences of a substring with a new text.
- Inputs:
Storage
bs:in string.replace:Replace data
str: Base string for replacements.
old: Substring to replace.
new: Replacement string.
maxreplace: Maximum replacements. (-1 for unlimited)
- Outputs:
Storage
bs:out string.replace: String after replacements.
Example: Replace world with minecraft in hello world then display the result::
data modify storage bs:in string.replace set value {str:"hello world",old:"world",new:"minecraft",maxreplace:-1}
function #bs.string:replace
# return "hello minecraft"
tellraw @a [{"text":"Result: \""},{"storage":"bs:out","nbt":"string.replace"},{"text":"\""}]
Technical Limitation
This function do not support the double quote character ("). Attempts to use or escape double quotes will result in errors.
Credits: Aure31
Replace Range#
- #bs.string:replace_range
Replace characters in a string between specified start and end positions with a new substring.
- Inputs:
Storage
bs:in string.replace_range:Replace range data
str: The original string to modify.
substr: The new string to insert between start and end positions.
start: Starting position in the original string (inclusive).
end: Ending position in the original string (exclusive).
- Outputs:
Storage
bs:out string.replace_range: The modified string with the specified range replaced.
Example: Replace the space in hello world with beautiful then display the result:
data modify storage bs:in string.replace_range set value {str:"hello world",substr:" beautiful ",start:5,end:6}
function #bs.string:replace_range
# return "hello beautiful world"
tellraw @a [{"text":"The new string is \""},{"storage":"bs:out","nbt":"string.replace_range"},{"text":"\""}]
How to Insert?
To insert text without removing any characters, set both start and end to the same position. This will insert the new text at that position while preserving all existing characters.
Technical Limitation
If you want to use double quotes (") within a string, you must escape them with a backslash: \".
Credits: Aure31
Reverse#
- #bs.string:reverse
Reverse the order of all characters in a string.
- Inputs:
Storage
bs:in string.reverse.str: The string to reverse.- Outputs:
Storage
bs:out string.reverse: The string with all characters in reverse order.
Example: Reverse hello world then display the result:
data modify storage bs:in string.reverse.str set value "hello world"
function #bs.string:reverse
# return "dlrow olleh"
tellraw @a [{"text":"The reversed string is \""},{"storage":"bs:out","nbt":"string.reverse"},{"text":"\""}]
Technical Limitation
This function do not support the double quote character ("). Attempts to use or escape double quotes will result in errors.
Credits: Aure31
Split#
- #bs.string:split
Divide a string into smaller parts using a specified separator, creating a list of substrings.
- Inputs:
Storage
bs:in string.split:Split data
str: The string to be split into parts.
separator: The character or string that marks where to split.
maxsplit: Maximum number of splits to perform (-1 for unlimited).
- Outputs:
Storage
bs:out string.split: A list containing the split parts of the original string, in the order they appear.
Example: Split hello world by " " then display the result:
data modify storage bs:in string.split set value {str:"hello world",separator:" ",maxsplit:-1}
function #bs.string:split
# return "["hello","world"]"
tellraw @a [{"text":"The list of strings is \""},{"storage":"bs:out","nbt":"string.split"},{"text":"\""}]
Technical Limitation
This function do not support the double quote character ("). Attempts to use or escape double quotes will result in errors.
Credits: Aure31
Transform Type#
- #bs.string:parse
Attempt to convert the string into its corresponding value.
- Inputs:
Storage
bs:in string.parse.str: The string containing the value to be extracted.- Outputs:
Storage
bs:out string.parse: The extracted value. Possible types: boolean, integer, double, string.
Example: Transform "42" into an integer then display the result:
data modify storage bs:in string.parse.str set value "42"
function #bs.string:parse
tellraw @a [{"text":"The number is \""},{"storage":"bs:out","nbt":"string.parse"},{"text":"\""}]
- #bs.string:to_list
Convert a string into a list where each character becomes a separate element.
- Inputs:
Storage
bs:in string.to_list.str: The string to convert into a character list.- Outputs:
Storage
bs:out string.to_list: A list containing each character as a separate element.
Example: Transform hello world into a character list then display the result:
data modify storage bs:in string.to_list.str set value "hello world"
function #bs.string:to_list
# return ["h","e","l","l","o"," ","w","o","r","l","d"]
tellraw @a [{"text":"The list of characters is \""},{"storage":"bs:out","nbt":"string.to_list"},{"text":"\""}]
- #bs.string:to_string
Convert any value into its string representation.
- Inputs:
Storage
bs:in string.to_string.value: The value to convert into a string.- Outputs:
Storage
bs:out string.to_string: The value converted to its string representation.
Example: Transform 42 into its string representation then display the result:
data modify storage bs:in string.to_string.number set value 42
function #bs.string:to_string
tellraw @a [{"text":"The string is \""},{"storage":"bs:out","nbt":"string.to_string"},{"text":"\""}]
Credits: Aure31
💬 Did it help you?
Feel free to leave your questions and feedback below!