We've split up the commands, scripting and mods, and add ons category! Please be sure you get your thread in the right place.

108

'/data modify [...] append' to concatenate two strings

11 Comments

Please sign in to leave a comment.

Sorted by oldest
  • 1
    Registered User commented
    Comment actions Permalink

    Would be nice for nbt array too

  • 4
    Registered User commented
    Comment actions Permalink

    This would be really useful for appending the player's UUID to the name field of a structure block e.g. "minecraft:some_structure/00000000-0000-0000-0000-000000000000". That way, you could allow each player to have a unique copy of a structure.

    I think this is a reasonable addition, since strings are treated as collections in many languages.

  • 6
    Registered User commented
    Comment actions Permalink

    I think strings should just be treated like an array of characters, so you could use append, prepend, insert, and also specify the [0] index.

  • 0
    Registered User commented
    Comment actions Permalink

    This would make them need to change strings to be treated as some sort of array-type. If you want to add elements to any array you need to use a value of the type that array will yield. In this case, maybe a new "character"-type? Otherwise saying "append string2 to string1" implies that you want to have an element within "string1" that is of a different type (an array-type again in this case) and that is not what you want.

  • 0
    Registered User commented
    Comment actions Permalink

    What I could see them do is add a new operation like "concat": data modify (block <pos>|entity <target>) <path> concat (from (block <pos>|entity <target>) [<path>]|value <value>)

    And both your paths and value need to yield a string ofc.

  • 4
    Registered User commented
    Comment actions Permalink

    Great Idea. Would be really useful when manipulating data. If numbers would also be automatically cast into Strings, like they are with the score selector in the chat or in signs, you could do some really interesting stuff with scores.

  • 1
    Registered User commented
    Comment actions Permalink

    FYI McTsts did some command trickery that allows you to convert a string to an array of characters. The only missing link is turning these characters back into a single string.

  • 1
    Registered User commented
    Comment actions Permalink

    Please don't forget this suggestion should probably include all these subcommands to work with strings:

    /data modify ... append
    /data modify ... insert <index>
    /data modify ... prepend

    And if you really want to go as far as supporting strings as character arrays:

    /data get ... someString[<charindex>]
    /data modify ... from ... someString[<charindex>]
    /data modify ... someString[<charindex>] set ...

     

  • 0
    Registered User commented
    Comment actions Permalink

    Agreed, but I suggest adding various operations for strings in all formal programming languages

  • 0
    Registered User commented
    Comment actions Permalink

    there may be a (very) complicated way to do it (theoretically) as of 1.19.4, since you can use the data command to splice strings. there is a method of turning an array of strings into a string called enchant flattening, but it gives you a bunch of useless characters before and after what you need. now you should be able to splice the desired concatenation out of the enchant command failure output.

    I am very much hopping that this works for a project of my own, but I have not tested it yet.

  • 1
    Registered User commented
    Comment actions Permalink

    I know this was posted several years ago, but now this is actually possible using macro functions.

    I made the following concatenate_two_strings.mcfunction :

    $data modify storage macro:concatenate result set value "$(string1)$(string2)"

    Given two strings, it stores the concatenated string in the storage.

     

    Example:

    /data modify storage macro:concatenate string1 set value "Hello "
    /data modify storage macro:concatenate string2 set value "World"
    /function mc_macros:concatenate_two_strings with storage macro:concatenate

    /data get storage macro:concatenate result
    # Output: Storage macro:concatenate has the following contents: "Hello World"