String: Difference between revisions

From Bohemia Interactive Community
Category: Data Types
(Page lifting)
No edit summary
 
(8 intermediate revisions by one other user not shown)
Line 21: Line 21:
| 2056
| 2056
| colspan="5" | No set limit
| colspan="5" | No set limit
| 9,999,999 ±
| 9,999,999 to 10,000,000<br>(same as [[Array]] length)
|-
|-
! Encoding
! Encoding
| [http://en.wikipedia.org/wiki/ASCII ASCII]
| {{Link|https://en.wikipedia.org/wiki/ASCII|ASCII}}
| colspan="6" | [http://en.wikipedia.org/wiki/Unicode Unicode]
| colspan="6" | {{Link|https://en.wikipedia.org/wiki/Unicode|Unicode}}
|-
|-
! [http://en.wikipedia.org/wiki/Code_page Code page]
! {{Link|https://en.wikipedia.org/wiki/Code_page|Code page}}
| product language defined
| product language defined
| colspan="6" | [http://en.wikipedia.org/wiki/UTF-8 UTF-8]
| colspan="6" | {{Link|https://en.wikipedia.org/wiki/UTF-8|UTF-8}}
|-
|-
! "text"
! "text"
Line 52: Line 52:
== Single quote limitation ==
== Single quote limitation ==


Other than double quotes ("text"), single quotes ('text') do not seem to support tabs:
Strings can be written with both double quotes ("text") and single quotes ('text'). While they are identical, there are slightly differences in how they are treated:
* Single quote strings are [[PreProcessor_Commands#Strings|parsed by the preprocessor]] and may cause undesired results.
* Single quote strings do not seem to support tabs:
<sqf>
_string = "
";
copyToClipboard _string;
diag_log format ["%1 %2 %3 x%4x", toArray _string, toArray copyFromClipboard, toArray (_string select [0]), _string];


_string = "
/*
";
return is good: "[9,10,9] [9,10,9] [9,10,9] x
[[copyToClipboard]] _string;
x"
[[diag_log]] [[format]] ["%1 %2 %3 x%4x", [[toArray]] _string, [[toArray]] [[copyFromClipboard]], [[toArray]] (_string [[select]] [0]), _string];
*/
</sqf>
{{codecomment|/*
return is good: "[9,10,9] [9,10,9] [9,10,9] x
x"
*/}}


_string = '
<sqf>
';
_string = '
[[copyToClipboard]] _string;
';
[[diag_log]] [[format]] ["%1 %2 %3 x%4x", [[toArray]] _string, [[toArray]] [[copyFromClipboard]], [[toArray]] (_string [[select]] [0]), _string];
copyToClipboard _string;
diag_log format ["%1 %2 %3 x%4x", toArray _string, toArray copyFromClipboard, toArray (_string select [0]), _string];
{{codecomment|/*
return: "[9,10] [9,10] [9,10] x
x" - second tab vanished
*/}}


<small>Example courtesy of [http://forums.bistudio.com/member.php?81568-Rydygier Rydygier]</small>
/*
return: "[9,10] [9,10] [9,10] x
x" - second tab vanished
*/
</sqf>
 
<small>Example courtesy of {{Link|http://forums.bistudio.com/member.php?81568-Rydygier|Rydygier}}</small>




== Examples ==
== Examples ==


=== Quotes in quotes ===
=== Quotes in Quotes ===


[[private]] _string = "my string ""with"" quotes";
<sqf>
[[private]] _string = 'my string "with" quotes';
private _string = "my string ""with"" quotes";
[[private]] _string = 'my string ''with'' quotes';
private _string = 'my string "with" quotes';
private _string = 'my string with quotes';
</sqf>


=== String formats ===
=== String Formats ===


{{cc|{{ofp}}}}
<sqf>
_string = "Hello there" {{cc|works}}
// Operation Flashpoint
_string = 'Hello there' {{cc|does NOT work}}
_string = "Hello there" // works
_string = {Hello there} {{cc|works}}
_string = 'Hello there' // does NOT work
_string = {Hello there} // works
</sqf>


{{cc|Since {{arma1}}}}
<sqf>
_string = "Hello there" {{cc|works}}
// since Armed Assault
_string = 'Hello there' {{cc|works}}
_string = "Hello there" // works
_string = {Hello there} {{cc|does NOT work}}
_string = 'Hello there' // works
_string = {Hello there} // does NOT work
</sqf>


=== Operators ===
=== Operators ===


[[private]] _finalString = "Hello" + " " + "there"; {{cc|outputs "Hello there" - the [[+]] sign concatenates strings}}
<sqf>private _finalString = "Hello" + " " + "there"; // outputs "Hello there" - the + sign concatenates strings</sqf>
The only basic operator to be used on strings is "plus". You can use "plus" to concatenate two strings.
The only basic operator to be used on strings is "plus". You can use "plus" to concatenate two strings.


=== Conversion ===
=== Conversion ===


[[private]] _value = 42; {{cc|this is a [[Number]]}}
<sqf>
[[hint]] _value; {{cc|error - [[hint]] takes a [[String]], not a [[Number]]}}
private _value = 42; // this is a Number
[[hint]] [[format]] ["%1", _value]; {{cc|ok}}
hint _value; // error - hint takes a String, not a Number
[[hint]] [[str]] _value; {{cc|ok - since {{arma1}}}}
hint format ["%1", _value]; // ok
hint str _value; // ok - since Armed Assault
</sqf>




[[Category: Data Types]]
[[Category: Data Types]]

Latest revision as of 10:59, 28 April 2025

A string is a variable type that contains text. Possible length and characters depend on the game and its version.

The encoding should be mostly transparent. The input files can be encoded both in UTF-16 or UTF-8, the toString and toArray functions always convert from/to UTF-16 representation.


Properties

OFP ArmA Arma 2 Arma 2:OA TKOH Arma 3 (before v1.56) Arma 3 (since v1.56)
Max length 2056 No set limit 9,999,999 to 10,000,000
(same as Array length)
Encoding ASCII Unicode
Code page product language defined UTF-8
"text" Checked
'text' Unchecked Checked
{text} Checked Unchecked


Commands and Functions


Single quote limitation

Strings can be written with both double quotes ("text") and single quotes ('text'). While they are identical, there are slightly differences in how they are treated:

  • Single quote strings are parsed by the preprocessor and may cause undesired results.
  • Single quote strings do not seem to support tabs:

_string = " "; copyToClipboard _string; diag_log format ["%1 %2 %3 x%4x", toArray _string, toArray copyFromClipboard, toArray (_string select [0]), _string]; /* return is good: "[9,10,9] [9,10,9] [9,10,9] x x" */

_string = ' '; copyToClipboard _string; diag_log format ["%1 %2 %3 x%4x", toArray _string, toArray copyFromClipboard, toArray (_string select [0]), _string]; /* return: "[9,10] [9,10] [9,10] x x" - second tab vanished */

Example courtesy of Rydygier


Examples

Quotes in Quotes

private _string = "my string ""with"" quotes"; private _string = 'my string "with" quotes'; private _string = 'my string with quotes';

String Formats

// Operation Flashpoint _string = "Hello there" // works _string = 'Hello there' // does NOT work _string = {Hello there} // works

// since Armed Assault _string = "Hello there" // works _string = 'Hello there' // works _string = {Hello there} // does NOT work

Operators

private _finalString = "Hello" + " " + "there"; // outputs "Hello there" - the + sign concatenates strings

The only basic operator to be used on strings is "plus". You can use "plus" to concatenate two strings.

Conversion

private _value = 42; // this is a Number hint _value; // error - hint takes a String, not a Number hint format ["%1", _value]; // ok hint str _value; // ok - since Armed Assault