Functions for modifying data strings. All functions provided are run using functions in a single Minecraft tick (no command blocks).
Once the data pack is installed, type /reload
in-game to load all data pack assets.
The data pack can be disabled in-game using the command /function ostr:uninstall
, whereafter the contents can be removed from the data packs folder.
Inputs and outputs to each function can be found in the ostr:io
NBT storage namespace. The namespace contains 2 modifiable tags:
in:{}
: the input(s) to a functionout
: the output from a function
The following functions are provided in this datapack:
ostr:concat
- Concatenate an array of strings into a single string in the same tickostr:slice
- Slice a string into a substring by indexostr:parse
- Parse a string into a character arrayostr:find
- Find substring(s) in a stringostr:replace
- Replace old substring(s) in a string with a new substringostr:int2hex
- Convert an int-array to a hexadecimal stringostr:hex2int
- Convert a hexadecimal string to an int-arrayostr:num2str
- Convert a number or numeric-array to a string
Each function takes the following arguments:
-
ostr:concat
- Concatenate an array of strings into a single string-
Input:
in:{array}
Parameters:
in.array
(List): an array of strings
-
Output (String): a concatenated string
-
Notes:
- Cannot concatente strings containing the characters
\"'
- Cannot concatente strings containing the characters
-
Example(s):
data modify storage ostr:io in.array set value ["hello", " world"] function ostr:concat # "hello world"
data modify storage ostr:io in.array set value ["tp minecraft:enderman[tag=", "test", "] ", "0 0 0"] function ostr:concat # "tp minecraft:enderman[tag=test] 0 0 0"
-
-
ostr:slice
- Slice a string into a substring by index-
Inputs:
in:{string, start: 0, end: 0}
Parameters:
in.string
(String): a stringin.start
(Int): start index (inclusive)in.end
(Int): end index (exclusive)
-
Output (String): a substring of
in.string
from start to end indeces -
Notes:
- Returns empty string if
in.start
is further along string thanin.end
- Negative indeces count from end of string (beginning from
-1
) - If
in.start
orin.end
are not provided, assumes value0
- Escape characters (
\
) add to string length and are present in output - Maximum output string length is
65536
- Returns empty string if
-
Example(s):
data modify storage ostr:io in set value {string:"abcd", start:0, end:2} function ostr:slice # "ab"
data modify storage ostr:io in set value {string:"ab\"cd", start:0, end:4} function ostr:slice # "ab\\""
data modify storage ostr:io in set value {string:"abcd", start:-2147483648, end:2147483647} function ostr:slice # "abcd"
data modify storage ostr:io in set value {string:"abcd", start:-3, end:-1} function ostr:slice # "bc"
-
-
ostr:parse
- Parse a string into a character array-
Input:
in:{string}
Parameters:
in.string
(String): a string
-
Output (List): an array of individual characters
-
Notes:
- Maximum parseable string length limited by
maxCommandChainLength
- Maximum parseable string length limited by
-
Example(s):
data modify storage ostr:io in.string set value "abc" function ostr:parse # ["a", "b", "c"]
-
-
ostr:find
- Find substring(s) in a string-
Input:
in:{string, value}
Parameters:
in.string
(String): a stringin.value
(String): the substring to find
-
Output (Int Array): An array of indeces where substring matches
-
Notes:
- Case sensitive
- Maximum searchable string length limited by
maxCommandChainLength
-
Example(s):
data modify storage ostr:io in set value {string:"hello world", value:"l"} function ostr:find # [I; 2, 3, 9]
data modify storage ostr:io in set value {string:"Lorem Ipsum", value:"i"} function ostr:find # [I;]
data modify storage ostr:io in set value {string:"aaaaaa", value:"aa"} function ostr:find # [I; 0, 1, 2, 3, 4]
-
-
ostr:replace
- Replace old substring(s) in a string with a new substring-
Input:
in:{string, old, new, count: 2147483647}
Parameters:
in.string
(String): a stringin.old
(String): the substring to be replacedin.new
(String): the substring to replace within.count
(Int): the number of substrings to replace
-
Output (String): a string
-
Notes:
- Case sensitive
- Cannot replace strings containing the characters
\"'
- If
in.count
is negative or not provided, assumes value2147483647
-
Example(s):
data modify storage ostr:io in set value {string:"hello world", old:"l", new:"w"} function ostr:replace # "hewwo worwd"
data modify storage ostr:io in set value {string:"hello there world", old:"there ", new:""} function ostr:replace # "hello world"
data modify storage ostr:io in set value {string:"ssssss", old:"ss", new:"t", count:2} function ostr:replace # "ttss"
-
-
ostr:int2hex
- Convert an int-array to a hexadecimal string-
Input:
in:{array, uuid: false}
Parameters:
in.array
(Int-array): an int-arrayin.uuid
(Boolean): treat input as a UUID int-array and return output in UUID hyphenated hex format
-
Output (List): a string in hex or hyphenated-hex format
-
Notes:
- If
uuid
is not specified, assumes valuefalse
- If
uuid
istrue
, uses last 4 values of int-array, and missing values assume0
- If
-
Example(s):
data modify storage ostr:io in set value {array:[I;123456789]} function ostr:int2hex # "75bcd15"
data modify storage ostr:io in set value {array:[I;-987654321, 123456789]} function ostr:int2hex # "c521974f75bcd15"
data modify storage ostr:io in set value {array:[I;-1119056911,-663994114,-1196126437,115991415], uuid: true} function ostr:int2hex # "bd4c8bf1-d86c-40fe-b8b4-8f1b06e9e377"
-
-
ostr:hex2int
- Convert a hexadecimal string to an int-array-
Input:
in:{string}
Parameters:
in.string
(String): a hexadecimal string
-
Output (List): an int-array
-
Notes:
- Skips any non-hex character (
0123456789abcdef
)
- Skips any non-hex character (
-
Example(s):
data modify storage ostr:io in set value {string:"abc123"} function ostr:hex2int # [I;11256099]
data modify storage ostr:io in set value {string:"e6b8d762-8147-4e53-9217-17b8b289e31f"} function ostr:hex2int # [I;-424093854,-2126033325,-1843980360,-1299586273]
-
-
ostr:num2str
- Convert a number or numeric-array to a string-
Input:
in:{value}
Parameters:
in.value
(Boolean or Byte or Short or Int or Long or Float or Double or Byte-array or Int-array or Long-array): a number or numeric-array
-
Output (String): a string representation of the number or numeric-array
-
Notes:
- Maximum output string length is
65536
- Maximum output string length is
-
Example(s):
data modify storage ostr:io in set value {value:123.456f} function ostr:num2str # "123.456f"
data modify storage ostr:io in set value {value:[I;1,2,3,4,5]} function ostr:num2str # "[I;1,2,3,4,5]"
-
See CREDITS.txt
MIT