String
functions
String.Append
(Formerly known as global function StrCat
, which is now obsolete)
String.Append(string str2)
Appends the string str2
to the end of the specified string, and returns the result.
IMPORTANT: The result of joining the strings together is returned as a new string from this command. The original string will NOT be changed. For example, the following script will not do anything:
mytext.Append("World");
what you probably want instead is:
mytext = mytext.Append("World");
Example:
String mytext = "Hello";
.Append("World");
mytext = mytextDisplay(mytext);
will display "HelloWorld".
See also: String.AppendChar
, String.Substring
, String.Truncate
String.AppendChar
String.AppendChar(int extraChar)
Appends a single character to the end of the specified string, and returns the result.
IMPORTANT: The newly extended text is returned as a new string from this command. The original string will NOT be changed. For example, the following script will not do anything:
mytext.AppendChar('o');
what you probably want instead is:
mytext = mytext.AppendChar('o');
Example:
String mytext = "Hell";
.AppendChar('o');
mytext = mytextDisplay(mytext);
will display "Hello".
Compatibility: This functions can take an utf-8 char as int
since AGS 3.6.0 and later versions.
See also: String.Append
String.CompareTo
(Formerly known as global function StrCaseComp
, which is now obsolete)
(Formerly known as global function StrComp
, which is now obsolete)
String.CompareTo(string str2, optional bool caseSensitive)
Compares the specified string to str2
. caseSensitive determines whether "Dog" and "dog" are equivalent; case sensitivity is off by default.
Returns 0 if the strings match, a number less than 0 if this string is earlier in the alphabet than str2
, and a number greater than 0 if this string is later in the alphabet than str2
.
TIP: To do a case-sensitive comparison of two strings, it's easier to just use the == operator.
Example:
String mytext = "Hello";
if (mytext.CompareTo("hello") == 0) {
Display("Strings match with case sensitivity off!");
}
else {
Display("Strings don't match with case sensitivity off!");
}
if (mytext == "hello") {
Display("Strings match with case sensitivity on!");
}
else {
Display("Strings don't match with case sensitivity on!");
}
will display "Strings match with case sensitivity off!", and then "Strings don't match with case sensitivity on!".
String.Copy
(Formerly known as global function StrCopy
, which is now obsolete)
String.Copy()
Returns a new copy of the specified string. You should not normally need to use this, since strings can be assigned using the = operator.
Example:
String mystring = "This is a test string.";
String newstring = mystring.Copy();
Display(newstring);
will display "This is a test string".
String.EndsWith
bool String.EndsWith(string lookForText, optional bool caseSensitive)
Returns true if this string ends with lookForText, or false if not.
caseSensitive is false by default, but you can set it to true so that the function will only return true for an exact-case match.
Example:
String myString = "Hello from the script!";
if (myString.EndsWith("script!"))
{
Display("Ends with script!");
}
will display the "Ends with script!" message.
Compatibility: Supported by AGS 3.1.0 and later versions.
See also: String.IndexOf
, String.StartsWith
String.Format
(Formerly known as global function StrFormat
, which is now obsolete)
static String.Format(string fmt, ...)
Processes the string FMT, optionally inserting values into the placeholders inside FMT, and returns the resulting string. You can insert the numeric or text, as well as value of variables into the string. Besides other purposes, this function is the method for converting numbers into the text.
For more information on how to work with format string, see the string formatting section.
NOTE: This function is static, which means you do not call it on an existing string variable, but use String.Format()
instead.
Examples:
// create a text string containing "10".
String text = String.Format("%d", 10);
// create a text string with health stats in it
int health, max_health;
String text = String.Format("Health: %d / %d", health, max_health);
// create a text string with character's name
String text = String.Format("Hello, my name is %s", player.Name);
See also: Display
String.IndexOf
(Formerly known as global function StrContains
, which is now obsolete)
(Formerly known as String.Contains
, which is now obsolete)
String.IndexOf(string needle)
Checks to see if NEEDLE is contained within the specified string. Returns the character position of the match if it is, or -1 if it is not.
This function is not case sensitive; i.e. testing "test string" for "sTRiN" would match.
Example:
String haystack = "The haystack had a needle in it somewhere.";
int result = haystack.IndexOf("a needle");
if (result == -1) {
Display("The string didn't contain the needle.");
}
else {
Display("a needle was found starting at character %d in the string.", result);
}
See also: String.EndsWith
, String.StartsWith
String.IsNullOrEmpty
static bool String.IsNullOrEmpty(String stringToCheck)
Returns whether the supplied string is null or empty. This is simply shorthand for the following:
if ((stringToCheck == null) || (stringToCheck == ""))
in other words, you can easily use this to check whether a string has any text in it or not.
NOTE: This function is static, which means you do not call it on an existing string variable, but use String.IsNullOrEmpty()
instead. See the example.
Example:
String myString;
if (String.IsNullOrEmpty(myString))
{
"Some text";
myString = }
will set the myString variable to "Some text" if it is null or empty (which it is).
Compatibility: Supported by AGS 3.0.1 and later versions.
String.LowerCase
(Formerly known as global function StrToLowerCase
, which is now obsolete)
String.LowerCase()
Returns a lower case version of the specified string.
NOTE: The new string is returned from this function; it does NOT modify the original string.
Example:
String mystring = "THIS is a test string";
String lowercased = mystring.LowerCase();
Display("Old: %s, new: %s", mystring, lowercased);
will display "Old: THIS is a test string, new: this is a test string".
See also: String.UpperCase
String.Replace
String.Replace(string lookForText, string replaceWithText,
bool caseSensitive) optional
Creates a copy of this string, with all instances of lookForText replaced with the replaceWithText.
caseSensitive is false by default, but you can set it to true so that only case-sensitive matches of the lookForText will be replaced.
NOTE: The new string is returned from this function; it does NOT modify the original string.
Example:
String original = "Hello from the script!";
String changed = original.Replace("hello", "goodbye");
Display("Old: %s, new: %s", original, changed);
will display "Old: Hello from the script!, new: goodbye from the script!".
Compatibility: Supported by AGS 3.1.0 and later versions.
See also: String.ReplaceCharAt
String.ReplaceCharAt
(Formerly known as global function StrSetCharAt
, which is now obsolete)
String.ReplaceCharAt(int index, int newChar)
Changes the character at INDEX in the string to NEWCHAR.
INDEX is the character index into the string (where 0 is the first character, and the last allowable value is the string's Length minus 1).
NOTE: The new string is returned from this function; it does NOT modify the original string.
Example:
String mystring = "Hello";
String changed = mystring.ReplaceCharAt(2, 'm');
Display("Old: %s, new: %s", newstring, changed);
will display "Old: Hello, new: Hemlo".
Compatibility: This functions can take an utf-8 char as int
since AGS 3.6.0 and later versions.
See also: String.Chars
, String.Replace
String.StartsWith
bool String.StartsWith(string lookForText, optional bool caseSensitive)
Returns true if this string starts with lookForText, or false if not.
caseSensitive is false by default, but you can set it to true so that the function will only return true for an exact-case match.
Example:
String myString = "Hello from the script!";
if (myString.StartsWith("hello"))
{
Display("Starts with hello!");
}
will display the "Starts with hello!" message.
Compatibility: Supported by AGS 3.1.0 and later versions.
See also: String.EndsWith
, String.IndexOf
String.Substring
String.Substring(int index, int length)
Returns part of the string, starting from character index and length characters long.
index is the initial character index, where 0 is the first character and (Length - 1) is the last. length specifies how many characters to retrieve.
Example:
String mystring = "Hello World!";
String substring = mystring.Substring(3, 5);
Display("Original: %s, Substring: %s", mystring, substring);
will display "Original: Hello World!, Substring: lo Wo".
See also: String.Append
, String.Chars
String.Truncate
String.Truncate(int length)
Returns a version of the string that has been truncated down to length characters.
NOTE: The new string is returned from this function; it does NOT modify the original string.
Example:
String mystring = "Hello World!";
String truncated = mystring.Truncate(4);
Display("Original: %s, Truncated: %s", mystring, truncated);
will display "Original: Hello World!, Truncated: Hell".
See also: String.Append
, String.Substring
String.UpperCase
(Formerly known as global function StrToUpperCase
, which is now obsolete)
String.UpperCase()
Returns an upper case version of the specified string.
NOTE: The new string is returned from this function; it does NOT modify the original string.
Example:
String mystring = "THIS is a test string";
String uppercased = mystring.UpperCase();
Display("Old: %s, new: %s", mystring, uppercased);
will display "Old: THIS is a test string, new: THIS IS A TEST STRING".
See also: String.LowerCase
String.AsFloat
readonly float String.AsFloat;
Converts the string into a float, and returns that value. Returns 0.0 if the string does not contain a number.
Example:
String text1, text2;
float number1,number2;
"57.362";
text1 = "Hello";
text2 = .AsFloat;
number1 = text1.AsFloat; number2 = text2
will set number1 value to 57.362 and number2 value to 0.0 This function is useful for processing strings input from the user.
NOTE: To convert a float to a string, you can use the String.Format
command.
See also: Game.InputBox
, String.AsInt
, String.Format
String.AsInt
(Formerly known as global function StringToInt
, which is now obsolete)
readonly int String.AsInt;
Converts the string into an integer, and returns that value. Returns zero if the string does not present a number.
NOTE: This operation takes just the leading sequence of the characters from the string, if it contains digits and other symbols which could be a part of number. If the string has the valid number only in the middle, it will not work.
Example:
String text1, text2;
int number1,number2;
"53";
text1 = "Hello";
text2 = .AsInt;
number1 = text1.AsInt; number2 = text2
will set number1 value to 53 and number2 value to 0. This function is useful for processing strings input from the user.
NOTE: To convert an integer to a string, you can use the String.Format
command.
See also: Game.InputBox
, String.AsFloat
, String.Format
String.Chars
(Formerly known as global function StrGetCharAt
, which is now obsolete)
readonly int String.Chars[position];
Returns the character at POSITION within the string.
POSITION is the character index (where 0 is the first character, and the last allowable value is the Length minus 1).
If POSITION is outside the string, this function returns 0.
NOTE: The Chars array is read-only. If you want to change one of the characters in the string, use String.ReplaceCharAt
.
Example:
String text = "This is my string.";
Display("The 4th character is: %c", text.Chars[3]);
will display "The 4th character is: s".
Compatibility: This attribute returns an utf-8 char as int
since AGS 3.6.0 and later versions.
See also: String.Length
, String.ReplaceCharAt
String.Length
(Formerly known as global function StrLen
, which is now obsolete)
readonly int String.Length;
Returns the length of the string, in characters.
Example:
String text = "This is my string.";
Display("Length: %d", text.Length);
will display "Length: 18".