diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2024-04-08 11:20:13 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2024-04-08 11:20:13 +0200 |
commit | 1b84421c9ba4cf9282d85004833edaa696c20bf1 (patch) | |
tree | 7b8f082dd6904b32f7ddc81213ec111c158e6c7a /doc | |
parent | bfccd57769d9be5a7c82105862c6c35c9a0df647 (diff) | |
parent | 8a78e7e174e4e1dc42fcefc6b01f9d9e3fbfb356 (diff) | |
download | redot-engine-1b84421c9ba4cf9282d85004833edaa696c20bf1.tar.gz |
Merge pull request #89608 from AThousandShips/format_doc_fix
[Doc] Clarify behavior of `String.format` with keys in replacements
Diffstat (limited to 'doc')
-rw-r--r-- | doc/classes/String.xml | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 7592342602..a33a1aea41 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -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> |