String

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.


String is a built-in type in ZScript. It is used for text data and has some additional features in it.

Special operations

You can use the .. operator to concatenate text representations of any objects (ints, doubles...) into a single string. Example:

 double x = 4;
 int y = 2;
 A_Log(x..", "..y); // outputs 4.00000, 2

Note that this way you cannot control the count of digits after the decimal point, for example. For that, see Format.

Methods

Static

  • int CharLower (int ch)
(Need more info)
  • int CharUpper (int ch)
(Need more info)
  • vararg String Format (String fmt, ...)
Allows to format a string using a subset of C Printf format.
 A_Log(String.Format("%p = %s (at %.2f, %.2f, %.2f)", self, GetClassName(), pos.x, pos.y, pos.z));
Note: uppercase formats generally mean uppercase versions of outputs.
Format Description
%% Escaped % character.
%s A string. Can be padded, for example, %-20s will mean that the string will be minimum 20 characters long.
%p A pointer. This takes an object, for example, an actor pointer like self.
%c An unsigned integer as character. Refer to the ASCII table for character indices.
%d, %i A signed integer as decimal. Signed integers can be negative. Can be padded with zeroes with formats like %04d (this particular one means minimum of 4 characters).
%u An unsigned integer as decimal. Unsigned integers cannot be negative.
%x, %X An unsigned integer as hexadecimal. %08X outputs uppercase 8-digit value padded with zeroes.
%o An unsigned integer as octal.
%f, %F A simple double format, for example, 1.82. Exact precision can be specified using %.2f (for guaranteed two digits after the decimal point).
%e, %E An exponent-based double format, for example, 2.00E-01 for 0.2. Exact precision for the main part can be specified the same way as for %f.
%g, %G Either %f/%F or %e/%E, depending on the situation and size of the number.
%a, %A A hexadecimal double format. This is not recommended to use, as it is OS-dependent.

Non-static

  • void AppendCharacter (int c)
(Need more info)
  • vararg void AppendFormat (String fmt, ...)
This is the same as Format, but it needs an existing string and appends the formatted value to it.
 String s;
 s.AppendFormat("%s %.2f", "meow", 0.24);
  • int ByteAt (int pos) const
(Need more info)
(Note: this has been deprecated in favor of ByteAt, CodePointCount and GetNextCodePoint.)
Returns the character at the specified position as string.
 String s = "abcd";
 String chrat = s.CharAt(1); // should be "b"
(Note: this has been deprecated in favor of ByteAt, CodePointCount and GetNextCodePoint.)
Returns the character at the specified position as an integer (ASCII code). This can be used with the %c format.
  • int CodePointCount () const
(Need more info)
  • void DeleteLastCharacter ()
(Need more info)
  • String Filter ()
(Need more info)
  • int, int GetNextCodePoint (int position) const
(Need more info)
  • int IndexOf (String substr, int startIndex = 0) const
Attempts to find and return the starting position of substr, if the string matches. This searches from left to right. This function is case sensitive. If there is no match, returns -1.
// Returns 5, since every character from left to right is a position.
// I.e. the I is at position 0, W at 1, a at 2, n at 3, t at 4, T at 5.
String tacoline = "IWantTacos";
int location = tacoline.IndexOf("Tacos");
  • int LastIndexOf (String substr, int endIndex = 2147483647) const (deprecated)
(Note: as of GZDoom 3.5.1, this function has been deprecated in favor of RightIndexOf due to incorrect implementation.)
Like IndexOf, but with a reversed search pattern going from right to left. This function is case sensitive.
  • String Left (int len) const
Like Mid, but always starts at the very beginning of the string.
 String s = "barkblargh";
 String bark = s.Left(4); // should result in a string "bark"
  • int Length ()
Returns how many characters are in the string.
String s = "abcd";
int size = s.Length(); // Returns 4.
  • String MakeLower () const
Returns the string converted to all lower-case letters.
  • String MakeUpper () const
Returns the string converted to all upper-case letters.
  • String Mid (int pos = 0, int len = 2147483647) const
Returns a substring starting at pos and len characters long, or, if len is too large, until the end of the string.
 String s = "tmeowd";
 String meow = s.Mid(1, 4); // should result in a string "meow"
  • void Remove (int index, int remlen)
(Need more info)
  • void Replace (String pattern, String replacement)
Replaces all occurrences of the specified pattern with replacement in an existing string.
 String s = "ZScript sucks";
 s.Replace("sucks", "is awesome");
 A_Log(s); // should output "ZScript is awesome"
  • int RightIndexOf (String substr, int endIndex = 2147483647) const
Like IndexOf, but with a reversed search pattern going from right to left. This function is case sensitive.
  • void Split (out Array<String> tokens, String delimiter, EmptyTokenType keepEmpty = TOK_KEEPEMPTY) const
Plugs pieces of the string into an array, separated by delimiter.
// Takes a list of possible actors and spawns one at random.
Array<String> tospawn;
string spawnlist = "BFG9000, Megasphere, Berserk, Backpack, Cyberdemon, InvulnerabilitySphere";
spawnlist.Split(tospawn, ", ");
Spawn(tospawn[random(0, tospawn.Size() - 1)], pos);
  • double ToDouble () const
(Need more info)
  • int ToInt (int base = 0) const
Converts a string of numbers into an integer of base base. If base is non zero, then the string will be treated as if it was a number of that base. If base is zero the string is used to determine the base.
string s = "012";
int i = s.ToInt(); // s = 10, this is an octal number
i = s.ToInt(10); // s = 12, this is a decimal number
(Note: this function has been deprecated in favor of MakeLower.)
Converts the string to all lower-case letters.
(Note: this function has been deprecated in favor of MakeUpper.)
Converts the string to all upper-case letters.
// Prints out a text string as upper case letters. If the font has upper and lower case,
// the difference will be visible. The default console font has both.
String t = "test";
Console.Printf("%s", t.ToUpper());
  • void Truncate (int newlen)
(Need more info)

See also