String: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(→‎Commands: added how to get the "length" of a string)
(Page lifting)
Line 1: Line 1:
{{TOC|side}}
{{TOC|side}}
= Description =
A '''string''' is the [[:Category:Data Types|variable type]] that can contain text and may consist of any number of ASCII characters and is enclosed by single-quotes (only in {{arma1}}) or double-quotes. In {{ofp}}, strings could alternatively also be written enclosed by curled braces, which are reserved for [[Code]] in {{arma1}}.


{{Feature | arma3 | From {{arma3}} v1.56 strings are limited to maximum of '''9,999,999''' (sometimes 10,000,000) characters}}
A '''string''' is a [[:Category:Data Types|variable type]] that contains text. Possible length and characters depend on the game and its version.


== Examples {{ofp}} ==
{{Feature|informative|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.}}
_string = "here is my string"
_string2 = {It may contain a lot of characters #@$}


== Examples {{arma1}} ==
_string = "here is my string"
_string2 = 'It may contain a lot of characters #@$'


If you want to include double quotes (") in strings enclosed by double quotes, the inside double quotes have to be written twice.
== Properties ==


_string = "my string ""with"" quotes"
{| class="wikitable" style="text-align: center"
_string2 = 'my other string "with" quotes'
!
! {{Name|ofp|short}}
! {{Name|arma1|short}}
! {{Name|arma2|short}}
! {{Name|arma2oa|short}}
! {{Name|tkoh|short}}
! {{Name|arma3|short}} (before v1.56)
! {{Name|arma3|short}} (since v1.56)
|-
! Max length
| 2056
| colspan="5" | No set limit
| 9,999,999 ±
|-
! Encoding
| [http://en.wikipedia.org/wiki/ASCII ASCII]
| colspan="6" | [http://en.wikipedia.org/wiki/Unicode Unicode]
|-
! [http://en.wikipedia.org/wiki/Code_page Code page]
| product language defined
| colspan="6" | [http://en.wikipedia.org/wiki/UTF-8 UTF-8]
|-
! "text"
| colspan="7" | {{Icon|checked}}
|-
! 'text'
| {{Icon|unchecked}}
| colspan="6" | {{Icon|checked}}
|-
! {text}
| {{Icon|checked}}
| colspan="6" | {{Icon|unchecked}}
|}


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


_string = "Hello " + "world"
== Commands and Functions ==


= Commands =
* [[:Category:Command Group: Strings|String Scripting Commands]]
== Type conversion ==
* [[:Category:Function Group: Strings|String Functions]]
You can convert any type to a string using the command [[format]]. You can also use that command to build together a string out of different elements. See the documentation of [[format]] for a closer description.


_string = [[format]] ["%1", ["my","array","of","strings"]];
[[hint]] _string;
=> ["my","array","of","strings"]


== Length ==
== Single quote limitation ==
 
_length = [[count]] "My tailor is rich";
[[hint]] [[str]] _length;
=> 17
 
= Limitations =
Since '''{{arma1}}''' strings seem not to have any limitation in length.
 
In '''{{ofp}} v1.96''', manipulating and using strings above 2056 characters in length may crash the game.


Other than double quotes ("text"), single quotes ('text') do not seem to support tabs:
Other than double quotes ("text"), single quotes ('text') do not seem to support tabs:
Line 51: Line 59:
  [[diag_log]] [[format]] ["%1 %2 %3 x%4x", [[toArray]] _string, [[toArray]] [[copyFromClipboard]], [[toArray]] (_string [[select]] [0]), _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
  {{codecomment|/*
  x"*/
return is good: "[9,10,9] [9,10,9] [9,10,9] x
  x"
*/}}


  _string = '
  _string = '
Line 59: Line 69:
  [[diag_log]] [[format]] ["%1 %2 %3 x%4x", [[toArray]] _string, [[toArray]] [[copyFromClipboard]], [[toArray]] (_string [[select]] [0]), _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
  {{codecomment|/*
  x" - second tab vanished*/
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>
 
 
== Examples ==


<small>Example by [http://forums.bistudio.com/member.php?81568-Rydygier Rydygier]</small>
=== Quotes in quotes ===


== Encoding ==
[[private]] _string = "my string ""with"" quotes";
In {{ofp}} the strings were internally [http://en.wikipedia.org/wiki/ASCII ASCII], and the [http://en.wikipedia.org/wiki/Code_page code page] used was defined by the product language version (by the fonts which were provided with it).
[[private]] _string = 'my string "with" quotes';
[[private]] _string = 'my string ''with'' quotes';


Since {{arma1}} all strings are [http://en.wikipedia.org/wiki/Unicode Unicode], with [http://en.wikipedia.org/wiki/UTF-8 UTF-8] encoding used internally.
=== String formats ===
 
{{cc|{{ofp}}}}
_string = "Hello there" {{cc|works}}
_string = 'Hello there' {{cc|does NOT work}}
_string = {Hello there} {{cc|works}}
 
{{cc|Since {{arma1}}}}
_string = "Hello there" {{cc|works}}
_string = 'Hello there' {{cc|works}}
_string = {Hello there} {{cc|does NOT work}}
 
=== Operators ===
 
[[private]] _finalString = "Hello" + " " + "there"; {{cc|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; {{cc|this is a [[Number]]}}
[[hint]] _value; {{cc|error - [[hint]] takes a [[String]], not a [[Number]]}}
[[hint]] [[format]] ["%1", _value]; {{cc|ok}}
[[hint]] [[str]] _value; {{cc|ok - since {{arma1}}}}


{{Feature|informative|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.}}


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

Revision as of 12:15, 5 May 2021

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 ±
Encoding ASCII Unicode
Code page product language defined UTF-8
"text" Checked
'text' Unchecked Checked
{text} Checked Unchecked


Commands and Functions


Single quote limitation

Other than double quotes ("text"), single quotes ('text') 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