Skip to content

Commit

Permalink
Merge pull request #89608 from AThousandShips/format_doc_fix
Browse files Browse the repository at this point in the history
[Doc] Clarify behavior of `String.format` with keys in replacements
  • Loading branch information
akien-mga committed Apr 8, 2024
2 parents bfccd57 + 8a78e7e commit 1b84421
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/classes/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@
print("User {id} is {name}.".format([["id", 42], ["name", "Godot"]]))
[/codeblock]
See also the [url=$DOCS_URL/tutorials/scripting/gdscript/gdscript_format_string.html]GDScript format string[/url] tutorial.
[b]Note:[/b] The replacement of placeholders is not done all at once, instead each placeholder is replaced in the order they are passed, this means that if one of the replacement strings contains a key it will also be replaced. This can be very powerful, but can also cause unexpected results if you are not careful. If you do not need to perform replacement in the replacement strings, make sure your replacements do not contain placeholders to ensure reliable results.
[codeblock]
print("{0} {1}".format(["{1}", "x"])) # Prints "x x".
print("{0} {1}".format(["x", "{0}"])) # Prints "x {0}".
print("{foo} {bar}".format({"foo": "{bar}", "bar": "baz"})) # Prints "baz baz".
print("{foo} {bar}".format({"bar": "baz", "foo": "{bar}"})) # Prints "{bar} baz".
[/codeblock]
[b]Note:[/b] In C#, it's recommended to [url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated]interpolate strings with "$"[/url], instead.
</description>
</method>
Expand Down

0 comments on commit 1b84421

Please sign in to comment.