Always snap to grid
Difference between revisions of "String"
(Redirecting to Types#String) |
(replaced redirect with actual information about the string type) |
||
Line 1: | Line 1: | ||
− | + | UnrealScript has a character string data type called '''string'''. Strings can contain any combination of [[wp:Unicode|Unicode]] characters. Note that the internal representation uses UTF-16 characters. UTF-8 is not recognized by the compiler or any of the built-in string operations. | |
+ | |||
+ | String values are immutable and UnrealScript neither provides "character" type nor allows direct access to individual string characters. There are, however, functions for extracting substrings and returning the Unicode value of the first character of a string. There's also a function for returning a string of length 1 containing a character corresponding to a specified Unicode value. | ||
+ | |||
+ | Very early builds of [[Unreal Engine 1]] had a fixed-length string type, which was declared with the syntax <code>string[''length'']</code>. This type is no longer supported and only mentioned here in case you run into its declaration in old code snippets. | ||
+ | |||
+ | In [[Unreal Engine 2]] (or at least [[UT2004]]) there also is an undocumented type called '''button''' that is an alias for the standard string type, but implies the [[Cache]] variable modifier. It is not actually used in stock code and there's no reason for you to ever declare a variable with the ''cache'' modifier, so this is purely informative. | ||
+ | |||
+ | ==Literals== | ||
+ | String literals start and end with double quotes. Between these there may be any number of characters, except line breaks or the null character. To include a double quote or backslash character in the string, it must be "escaped" by a(nother) backslash character. Starting with Unreal Engine 3, strings can also contain other escape sequences, such as \n for a newline character. In Unreal Engine 1 and 2 escaped letters only stand for themselves, i.e. the string "\n" is absolutely equal to the string "n" there. | ||
+ | |||
+ | '''Examples:''' | ||
+ | :<code>"abc"</code> | ||
+ | :<code>"This is an \"example\"." | ||
+ | :<code>""</code> (the empty string and default string value) | ||
+ | |||
+ | Technically string literals are allowed to have a length of up to 1023 characters. String values generated at runtime may be longer. Internally strings are zero-terminated, which means no string can contain the null character because it would be recognized as the end of the string. | ||
+ | |||
+ | ==String operators== | ||
+ | The following [[operators]] are commonly declared in the {{classgames|Object}} class and thus are available globally: | ||
+ | |||
+ | ;<code>==</code>:Case-sensitive equality comparison. Returns ''true'' [[wp:iff|iff]] the string expression on the left and right are identical. | ||
+ | ;<code>~=</code>:Case-insensitive equality comparison. Returns ''true'' iff the string expression on the left and right are identical, ignoring any differences in the case of the letters A-Z. Note that non-ASCII characters will be matched case-sensitively, i.e. <code>"a" ~= "A"</code> is ''true'', but <code>"Ä" ~= "ä"</code> is ''false''. | ||
+ | ;<code><</code>:Case-sensitive lexicographical comparison. Returns ''true'' iff the string expression on the left comes lexicographically before the string expression on the right. | ||
+ | ;<code>></code>:Case-sensitive lexicographical comparison. Returns ''true'' iff the string expression on the left comes lexicographically after the string expression on the right. | ||
+ | ;<code><=</code>:Case-sensitive lexicographical comparison. Returns ''false'' iff the string expression on the left comes lexicographically after the string expression on the right. | ||
+ | ;<code>>=</code>:Case-sensitive lexicographical comparison. Returns ''false'' iff the string expression on the left comes lexicographically before the string expression on the right. | ||
+ | ;<code>!=</code>:Case-sensitive inequality comparison. Returns ''false'' iff the string expression on the left and right are identical. | ||
+ | ;<code>$</code>:String concatenation. <code>"abc" $ "def"</code> evaluates to the value <code>"abcdef"</code> at runtime. Note that the UnrealScript compiler will not optimize this kind of expression. This operator will "[[coerce]]" (i.e. automatically typecast) its operands to type string. | ||
+ | ;<code>@</code>:Spaced string concatenation. The expression <code>a @ b</code> essentially corresponds to <code>a $ " " $ b</code>, except that it is slightly more efficient. Still you may want to limit its use to concatinating two non-literal expressions, as <code>"abc " $ d</code> is slightly more efficient than <code>"abc" @ d</code>. This operator will "coerce" (i.e. automatically typecast) its operands to type string. | ||
+ | ;<code>$=</code><sup>2+</sup>:Combined concatenation and assignment. Left operand must be an assignable expression, i.e. a variable, struct member or array element. This operator will "coerce" (i.e. automatically typecast) the right operand to type string. | ||
+ | ;<code>@=</code><sup>2+</sup>:Combined spaced concatenation and assignment. Left operand must be an assignable expression, i.e. a variable, struct member or array element. Like for the <code>@</code> operator, <code>foo $= " bar"</code> is slightly more efficient than <code>foo @= "bar"</code>. This operator will "coerce" (i.e. automatically typecast) the right operand to type string. | ||
+ | ;<code>-=</code><sup>2+</sup>:Substring removal. This operator will remove all substrings from the variable, struct member or array element on the left side that match the string expression on the right side case-sensitively. <code>foo -= "an"</code> will turn the string "B<u>anan</u>a An<u>an</u>as" into "Ba Anas". This operator will "coerce" (i.e. automatically typecast) the right operand to type string. Note that there is no operator <code>-</code> for strings. <code>a -= b;</code> is essentially the same as <code>a = Repl(a, b, "", False);</code>. | ||
+ | ;<code>=</code>: The build in assignment operator. Assigns the string value of the right side operand to the variable, struct member or array element represented by the left operand. Note that this operator does ''not'' "coerce" any operands and unlike the combined operators also does not return any value. | ||
+ | |||
+ | ==Global string functions== | ||
+ | The following [[static functions|static]] string functions are commonly declared in the {{classgames|Object}} class and thus are available globally: | ||
+ | |||
+ | ;<code>int Len(coerce string S)</code>:Returns the number of characters in the specified string value. | ||
+ | ;<code>int InStr(coerce string S, coerce string T)</code>:Locates the first case-sensitive match of ''T'' in ''S'' and returns its zero-based start position. Returns -1 if ''T'' cannot be found in ''S''. | ||
+ | ;<code>string Mid(coerce string S, int start, optional int length)</code>:Returns the substring of ''S'' starting at position ''start'', including up to ''length'' characters. If ''length'' is omitted, all characters up to the end of the string are returned. | ||
+ | ;<code>string Left(coerce string S, int i)</code>:Returns the first ''i'' (i.e. left-most) characters from ''S''. | ||
+ | ;<code>string Right(coerce string S, int i)</code>:Returns the last ''i'' (i.e. right-most) characters from ''S''. | ||
+ | ;<code>string Caps(coerce string S)</code>:Returns ''S'' with all lowercase ASCII letters (a-z) converted to their uppercase forms (A-Z). | ||
+ | ;<code>string Locs(coerce string S)</code><sup>2+</sup>:Returns ''S'' with all uppercase ASCII letters (A-Z) converted to their lowercase forms (a-z). | ||
+ | ;<code>string Chr(int i)</code>:Returns a string of length 1 containing the Unicode character with the specified character code. Note that the empty string will be returned when specifying 0 as code. | ||
+ | ;<code>int Asc(string S)</code>:Returns the Unicode character code of the first character in ''S''. This function will return 0 for the empty string. Note that unlike most other global string function, ''Asc'' does not "[[coerce]]" (auto-convert) its parameter to type string. | ||
+ | ;<code>string Repl(coerce string Src, coerce string Match, coerce string With, optional bool bCaseSensitive)</code><sup>2+</sup>:Replaces every occurrence of ''Match'' in ''Src'' with ''With'' using case-sensitive or case-insensitive (this is the default) matching. | ||
+ | |||
+ | {{navbox unrealscript}} |
Latest revision as of 12:51, 29 August 2015
UnrealScript has a character string data type called string. Strings can contain any combination of Unicode characters. Note that the internal representation uses UTF-16 characters. UTF-8 is not recognized by the compiler or any of the built-in string operations.
String values are immutable and UnrealScript neither provides "character" type nor allows direct access to individual string characters. There are, however, functions for extracting substrings and returning the Unicode value of the first character of a string. There's also a function for returning a string of length 1 containing a character corresponding to a specified Unicode value.
Very early builds of Unreal Engine 1 had a fixed-length string type, which was declared with the syntax string[length]
. This type is no longer supported and only mentioned here in case you run into its declaration in old code snippets.
In Unreal Engine 2 (or at least UT2004) there also is an undocumented type called button that is an alias for the standard string type, but implies the Cache variable modifier. It is not actually used in stock code and there's no reason for you to ever declare a variable with the cache modifier, so this is purely informative.
Literals[edit]
String literals start and end with double quotes. Between these there may be any number of characters, except line breaks or the null character. To include a double quote or backslash character in the string, it must be "escaped" by a(nother) backslash character. Starting with Unreal Engine 3, strings can also contain other escape sequences, such as \n for a newline character. In Unreal Engine 1 and 2 escaped letters only stand for themselves, i.e. the string "\n" is absolutely equal to the string "n" there.
Examples:
"abc"
"This is an \"example\"."
""
(the empty string and default string value)
Technically string literals are allowed to have a length of up to 1023 characters. String values generated at runtime may be longer. Internally strings are zero-terminated, which means no string can contain the null character because it would be recognized as the end of the string.
String operators[edit]
The following operators are commonly declared in the Object(RTNP, U1, UT, U2, U2XMP, UE2Runtime, UT2003, UT2004, UDK, UT3) class and thus are available globally:
==
- Case-sensitive equality comparison. Returns true iff the string expression on the left and right are identical.
~=
- Case-insensitive equality comparison. Returns true iff the string expression on the left and right are identical, ignoring any differences in the case of the letters A-Z. Note that non-ASCII characters will be matched case-sensitively, i.e.
"a" ~= "A"
is true, but"Ä" ~= "ä"
is false. <
- Case-sensitive lexicographical comparison. Returns true iff the string expression on the left comes lexicographically before the string expression on the right.
>
- Case-sensitive lexicographical comparison. Returns true iff the string expression on the left comes lexicographically after the string expression on the right.
<=
- Case-sensitive lexicographical comparison. Returns false iff the string expression on the left comes lexicographically after the string expression on the right.
>=
- Case-sensitive lexicographical comparison. Returns false iff the string expression on the left comes lexicographically before the string expression on the right.
!=
- Case-sensitive inequality comparison. Returns false iff the string expression on the left and right are identical.
$
- String concatenation.
"abc" $ "def"
evaluates to the value"abcdef"
at runtime. Note that the UnrealScript compiler will not optimize this kind of expression. This operator will "coerce" (i.e. automatically typecast) its operands to type string. @
- Spaced string concatenation. The expression
a @ b
essentially corresponds toa $ " " $ b
, except that it is slightly more efficient. Still you may want to limit its use to concatinating two non-literal expressions, as"abc " $ d
is slightly more efficient than"abc" @ d
. This operator will "coerce" (i.e. automatically typecast) its operands to type string. $=
2+- Combined concatenation and assignment. Left operand must be an assignable expression, i.e. a variable, struct member or array element. This operator will "coerce" (i.e. automatically typecast) the right operand to type string.
@=
2+- Combined spaced concatenation and assignment. Left operand must be an assignable expression, i.e. a variable, struct member or array element. Like for the
@
operator,foo $= " bar"
is slightly more efficient thanfoo @= "bar"
. This operator will "coerce" (i.e. automatically typecast) the right operand to type string. -=
2+- Substring removal. This operator will remove all substrings from the variable, struct member or array element on the left side that match the string expression on the right side case-sensitively.
foo -= "an"
will turn the string "Banana Ananas" into "Ba Anas". This operator will "coerce" (i.e. automatically typecast) the right operand to type string. Note that there is no operator-
for strings.a -= b;
is essentially the same asa = Repl(a, b, "", False);
. =
- The build in assignment operator. Assigns the string value of the right side operand to the variable, struct member or array element represented by the left operand. Note that this operator does not "coerce" any operands and unlike the combined operators also does not return any value.
Global string functions[edit]
The following static string functions are commonly declared in the Object(RTNP, U1, UT, U2, U2XMP, UE2Runtime, UT2003, UT2004, UDK, UT3) class and thus are available globally:
int Len(coerce string S)
- Returns the number of characters in the specified string value.
int InStr(coerce string S, coerce string T)
- Locates the first case-sensitive match of T in S and returns its zero-based start position. Returns -1 if T cannot be found in S.
string Mid(coerce string S, int start, optional int length)
- Returns the substring of S starting at position start, including up to length characters. If length is omitted, all characters up to the end of the string are returned.
string Left(coerce string S, int i)
- Returns the first i (i.e. left-most) characters from S.
string Right(coerce string S, int i)
- Returns the last i (i.e. right-most) characters from S.
string Caps(coerce string S)
- Returns S with all lowercase ASCII letters (a-z) converted to their uppercase forms (A-Z).
string Locs(coerce string S)
2+- Returns S with all uppercase ASCII letters (A-Z) converted to their lowercase forms (a-z).
string Chr(int i)
- Returns a string of length 1 containing the Unicode character with the specified character code. Note that the empty string will be returned when specifying 0 as code.
int Asc(string S)
- Returns the Unicode character code of the first character in S. This function will return 0 for the empty string. Note that unlike most other global string function, Asc does not "coerce" (auto-convert) its parameter to type string.
string Repl(coerce string Src, coerce string Match, coerce string With, optional bool bCaseSensitive)
2+- Replaces every occurrence of Match in Src with With using case-sensitive or case-insensitive (this is the default) matching.
Declarations | Preprocessor • Classes • Interfaces • Cpptext • Constants • Enums • Structs • Variables (Metadata) • Replication block • Operators • Delegates • Functions • States • Defaultproperties (Subobjects) |
---|---|
Types | bool • byte • float • int • name • string • Object • Class • Enums • Structs (Vector ⋅ Rotator ⋅ Quat ⋅ Color) • Static arrays • Dynamic arrays • Delegates • Typecasting |
Literals | Boolean • Float • Integer • Names • Objects (None ⋅ Self) • Vectors • Rotators • Strings |
Flow | GoTo • If • Assert • Return • Stop • Switch • While • Do...Until • For • ForEach • Break • Continue |
Specifiers | Super • Global • Static • Default • Const |
UnrealScript | Syntax • .UC • .UCI • .UPKG • Comments • #directives • Native |