format: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(85 intermediate revisions by 17 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| ofp |= Game name
|game1= ofp
|version1= 1.00


|1.00|= Game version
|game2= ofpe
____________________________________________________________________________________________
|version2= 1.00


| Composes a string containing other variables or other variable types.
|game3= arma1
|version3= 1.00


Converts any variable type to a string.
|game4= arma2
|version4= 1.00


If you want to convert a string back to a number, use [[call_body|call]].
|game5= arma2oa
|version5= 1.50


The array used with this command has to contain at least two items.|= Description
|game6= tkoh
____________________________________________________________________________________________
|version6= 1.00


| [[String]] <nowiki>=</nowiki> '''format''' ["FormatString", var1, var2 ...] |= Syntax
|game7= arma3
|version7= 0.50


|p1= FormatString: [[String]] - a string containing references to the variables listed below in the array. The references appear in form of %1, %2 etc. |= Parameter 1
|gr1= Strings


|p2= var1: [[Any Value]] - variable referenced by %1 |= Parameter 2
|descr= Composes a string containing other variables or other variable types. Converts any variable type to a string. If you want to convert a string back to a number, use [[parseNumber]].
{{Feature | important | The output of this command is limited to ~8Kb. [[joinString]] on the other hand doesn't have such limit and is a bit faster.}}


|p3= var2: [[Any Value]] - variable referenced by %2, and so on |= Parameter 3
{{Feature|informative|Since {{GVI|arma3|2.20}} the maximum length of the string is {{hl|8388608}} characters. Additionally, two consecutive percent symbols will be formatted to a single percent symbol in the final string {{hl|(%% -> %)}}}}


| [[String]]
|s1= [[format]] [formatString, var1, var2, ...]
The full string is returned. |= Return value
____________________________________________________________________________________________
 
|x1= <pre>format ["Player side: %1 - Human players on that side: %2", side player, playersNumber side player]</pre>


returns "Player side: West - Human players on that side: 1" |= Example 1
|p1= formatString: [[String]] - a string containing text and/or references to the variables listed below in the array. The references appear in the form of %1, %2 etc.
____________________________________________________________________________________________


| |= See also
|p2= varN: [[Anything]] - variable referenced by %N


}}
|r1= [[String]] - the full string (up to 8kb, see above) is returned.


<h3 style="display:none">Notes</h3>
|x1= <sqf>format ["Player:%1, player's side:%2", player, side player]; // returns "Player:WEST 1-1-A:1 (Username), player's side:WEST"</sqf>
<dl class="command_description">
<!-- Note Section BEGIN -->


<dd class="notedate"></dd>
|x2= <sqf>
<dt class="note">'''Unknown'''</dt><dd class="note">The format command is very strong in combination with [[call_body|call]].  Dynamic code can be created using format and then executed with call.</dd>
player addEventHandler ["HandleDamage", {
hint format ["You just sustained %1%2 damage!", ceil ((_this select 2) * 100), "%"];
}]; // Shows "You just sustained 20% damage!"
</sqf>


<dd class="notedate">Posted on August 5, 2006 - 14:12</dd>
|x3= <sqf>
<dt class="note">'''[[User:Hardrock|hardrock]]'''</dt><dd class="note">''Notes from before the conversion:''
// Since Arma 3 v2.18 two consecutive percent symbols will be formatted to a single percent symbol in the final string (%% -> %)
player addEventHandler ["HandleDamage", {
hint format ["You just sustained %1%% damage!", ceil ((_this select 2) * 100)];
}]; // Shows "You just sustained 20% damage!"
</sqf>


It is not advisable to use this command with string arguments only. Better use + for string concatenation then. Read more in [[String operators]]
|seealso= [[formatText]] [[endl]] [[str]] [[toString]] [[joinString]]
}}


If variables don't exist, their value as string is generally "scalar bool array string 0xfcffffef"
{{Note
</dd>
|user= Worldeater
|timestamp= 20090302151200
|text= The maximum number of characters returned by the ''format'' command seems to depend on the total byte count. For plain ASCII strings the limit is 2048 characters.
|game= arma2
}}


<dt class="note">'''[[User:Dr_Eyeball|Dr_Eyeball]]'''</dt><dd class="note">Object array parameters work very nicely.
{{Note
Eg:
|user= Heeeere's Johnny!
_MyArray = [Obj1, Obj2, Obj3];
|timestamp= 20150328092200
Hint Format["MyArray=%1", _MyArray];
|text= In addition to the note above, [[formatText]]ed strings are [[Structured Text]], hence cannot be used like [[format]]ted strings (with most scripting commands). A workaround to get strings of type [[String]] with more than 8191 characters is:
</dd>
<sqf>str formatText ["%1", _string]</sqf>
 
}}
<dd class="notedate">Posted on August 5, 2006 - 14:12</dd>
 
<!-- Note Section END -->
</dl>
 
<h3 style="display:none">Bottom Section</h3>
[[Category:Scripting Commands|FORMAT]]
[[Category:Scripting Commands OFP 1.96|FORMAT]]
[[Category:Scripting Commands OFP 1.46|FORMAT]]
[[Category:Scripting Commands ArmA|FORMAT]]
[[Category:Command_Group:_Interaction|{{uc:{{PAGENAME}}}}]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]

Latest revision as of 16:18, 3 November 2024

Hover & click on the images for description

Description

Description:
Composes a string containing other variables or other variable types. Converts any variable type to a string. If you want to convert a string back to a number, use parseNumber.
The output of this command is limited to ~8Kb. joinString on the other hand doesn't have such limit and is a bit faster.
Since Arma 3 logo black.png2.20 the maximum length of the string is 8388608 characters. Additionally, two consecutive percent symbols will be formatted to a single percent symbol in the final string (%% -> %)
Groups:
Strings

Syntax

Syntax:
format [formatString, var1, var2, ...]
Parameters:
formatString: String - a string containing text and/or references to the variables listed below in the array. The references appear in the form of %1, %2 etc.
varN: Anything - variable referenced by %N
Return Value:
String - the full string (up to 8kb, see above) is returned.

Examples

Example 1:
format ["Player:%1, player's side:%2", player, side player]; // returns "Player:WEST 1-1-A:1 (Username), player's side:WEST"
Example 2:
player addEventHandler ["HandleDamage", { hint format ["You just sustained %1%2 damage!", ceil ((_this select 2) * 100), "%"]; }]; // Shows "You just sustained 20% damage!"
Example 3:
// Since Arma 3 v2.18 two consecutive percent symbols will be formatted to a single percent symbol in the final string (%% -> %) player addEventHandler ["HandleDamage", { hint format ["You just sustained %1%% damage!", ceil ((_this select 2) * 100)]; }]; // Shows "You just sustained 20% damage!"

Additional Information

See also:
formatText endl str toString joinString

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Worldeater - c
Posted on Mar 02, 2009 - 15:12 (UTC)

The maximum number of characters returned by the format command seems to depend on the total byte count. For plain ASCII strings the limit is 2048 characters.

Heeeere's Johnny! - c
Posted on Mar 28, 2015 - 09:22 (UTC)
In addition to the note above, formatTexted strings are Structured Text, hence cannot be used like formatted strings (with most scripting commands). A workaround to get strings of type String with more than 8191 characters is:
str formatText ["%1", _string]