String

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


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

Special operations

You can use 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 can't control the count of digits after the decimal point, for example. For that, see Format.

Static methods

  • String Format(String fmt, ...) — allows to format a string using a subset of C Printf format.

This is a static method, which means that you call it off the class, like this:

 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 can't 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's OS-dependent.

Methods

  • void AppendFormat(String fmt, ...) — the same as regular Format, but it needs an existing string and appends the formatted value to it.
 String s;
 s.AppendFormat("%s %.2f", "meow", 0.24);
  • void Replace(String pattern, String replacement) — replaces all occurrences of 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"
  • String Mid(int pos = 0, int len = 2147483647) — 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"
  • String Left(int len) - 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"
  • String CharAt(int pos) — returns single character at the specified position as string.
 String s = "abcd";
 String chrat = s.CharAt(1); // should be "b"
  • int CharCodeAt(int pos) — returns single character at the specified position as integer (ASCII code). This can be used with %c format.
  • int Length() - returns how many characters are in the string.
String s = "abcd";
int size = s.Length(); // Returns 4.
  • int IndexOf(String substr, int startIndex = 0) - 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.
  • int LastIndexOf(String substr) - Like IndexOf, but with a reversed search pattern going from right to left. This function is case sensitive.
// 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");
  • void ToLower() - converts the string to all lower-case letters.
  • void ToUpper() - 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 Split(array<string> tokens, string delimiter, EmptyTokenType keepEmpty = true) - 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);
  • int ToInt(int base = 0) - (Need more info)
  • double ToDouble() - (Need more info)

See Also