Class Str

From TrainzOnline
Jump to: navigation, search


  • Str is a static class providing string utility functions not supported by the native string object.
  • When used in game code these methods must be prefixed Str. to reference the class.
  • Some of these methods directly amend their parameters rather than returning values via the method.


Contents

Left

public void Left(string s, int count)
Parameters
  • s = string to be processed.
  • count = count of characters to retain.
Returned Value
  • None
Syntax
string s = "Afternoon";
Str.Left(s,5);        // s will now be equal to "After"
Notes
  • Sets s to be a substring of itself consisting of its first count characters.
  • Method does not return a value.


Mid

public void Mid(string s, int first, int count)
Parameters
  • s = string to be processed.
  • first = index of first character to retain.
  • count = number of characters to retain.
Returned Value
  • None
Syntax
string s = "Afternoon";
Str.Mid(s,3,4);           // s will now be equal to "tern"
Notes
  • Sets s to be a substring of itself count characters long starting with the first character.
  • Method does not return a value.


To get the output specified you would need the following: Str.Mid(s,2,4); as the string array is zero based. This has been tested using TS2012 build 61388 and TANE.

Right

public void Right(string s, int count)
Parameters
  • s = string to be processed.
  • count = number of characters to retain.
Returned Value
  • None
Syntax
string s = "Afternoon";
Str.Right(s,4);           // s will now be equal to "noon"
Notes
  • Sets s to be a substring of itself composed of the last count characters.
  • Method does not return a value.


Find

Find(string s, string substr, int startingIndex)
Parameters
  • s = string in which to search
  • substr - substring to find
  • startingIndex - where to start looking down the string i.e. skip the first 'n' characters
Returned Value
  • index within string that substr starts at. '-1' if not found.
Syntax
int i = Str.Find("amazon", "z", 0) //returns 3
Notes
  • Searches for a substring within a string.


ToInt

public native int ToInt(string s)
Parameters
  • s = string parameter, which should be a string representation of an integer.
Returned Value
  • Integer value of string parameter.
Syntax
int n = Str.ToInt("1234");
Notes
  • Converts s to an integer.
  • Method returns 0 if the string cannot be converted.


ToFloat

public native float ToFloat(string s)
Parameters
  • s = string parameter, which should be a string representation of a floating point number.
Returned Value
  • Floating point value.
Syntax
float length = Str.ToFloat("1.5");
Notes
  • Converts s to a floating point number.
  • Method returns 0.0 if the string cannot be converted.


Tokens

public native string[ ] Tokens(string s, string delimiters)
Parameters
  • s = string to be processed.
  • delimiters = string of characters to use as token boundaries.
Returned Value
  • Array of string tokens.
Syntax
string[] sentence = Str.Tokens("Once upon a time","");     // sentence contains an array of four strings:
                                                           // ["Once","upon","a","time"].
int wordCount = Str.Tokens("Once upon a time","").size();  // returns 4
string firstWord = Str.Tokens("Once upon a time","")[0];   // returns "Once"

string kuidStr = "<kuid:1234:5678>"
string userID = Str.Tokens(kuidStr,"<:>")[1];              // returns "1234"
Notes
  • Splits a string into an array of tokens using any of the characters in delimiters as token boundaries.
  • Characters contained within the delimiters string are never returned.
  • If delimiters is null or an empty string then white space will be assumed to define the token boundaries.


ToLower

public native void ToLower(string s)
Parameters
  • s = string to be processed.
Returned Value
  • None
Syntax
Str.ToLower("UPPER CASE STRING");
Notes
  • Converts the string parameter s to lower case.
  • Method does not return a value.


ToUpper

public native void ToUpper(string s)
Parameters
  • s = string to be processed.
Returned Value
  • None
Syntax
Str.ToUpper("lower case string");
Notes
  • Converts the string parameter s to upper case.
  • Method does not return a value.


TrimLeft

public native void TrimLeft(string s, string trim)
Parameters
  • s = string to be processed.
  • trim = characters to remove
Returned Value
  • None
Syntax
Str.TrimLeft("Once upon a time","Once ");      // returns "upon a time"
Str.TrimLeft("      result","");               // returns "result"
Notes
  • If trim is an empty string then all white space will be stripped from the front of the string.
  • Method does not return a value.


TrimRight

public native void TrimRight(string s, string trim)
Parameters
  • s = string to be processed.
  • trim = characters to remove
Returned Value
  • None
Syntax
Str.TrimRight("Once upon a time"," a time");      // returns "Once upon"
Str.TrimRight("result        ","");               // returns "result
Notes
  • If trim is an empty string then all white space will be stripped from the end of the string.
  • Method does not return a value.


UnpackInt

public native int UnpackInt(string s)
Parameters
  • s = string to be processed.
Returned Value
  • First integer value in s.
Syntax
int n = Str.UnpackInt("21st May 2008");           // returns 21 and amends s to "st May 2008" 
Notes
  • Strips the first integer value found from the string parameter and returns it in the method result.


UnpackFloat

public native float UnpackFloat(string s)
Parameters
  • s = string to be processed.
Returned Value
  • First float value in s.
Syntax
float distance = Str.UnpackFloat("21.5 miles");   // returns 21.5 and amends s to " miles".
Notes
  • Strips the first floating point value found from the string parameter and returns it in the method result.


UnpackString

public native string UnpackString(string s)
Parameters
  • s = string to be processed.
Returned Value
  • First string in s, excluding any leading white space.
Syntax
string s = "Once upon a time";
string firstWord = UnpackString(s);     // returns "Once", s becomes " upon a time"
string secondWord = UnpackString(s);    // returns "upon", s becomes " a time"
string thirdWord = UnpackString(s);     // returns "a",    s becomes " time"
string fourthWord = UnpackString(s);    // returns "time", s becomes ""
Notes
  • Strips the first word from the string parameter (using white space for word separation) and returns it in the method result.
  • Any leading white space is removed from the string before parsing.


CloneString

public string CloneString(string s)
Parameters
  • s = The string to clone.
Returned Value
  • string - A new string in the current gamescript context which has contents that exactly match the original string.
Syntax
string original="Hello World!"
string copy;
copy=Str.CloneString(original);
Notes
  • CloneString() creates a copy of the string passed.
  • Ordinarily 'copying' a string will just create a new object that references the old string. This is not allowed across different contexts.
  • CloneString() creates a new string for use in the current context.


Related Methods

TrainUtil.AlreadyThereStr()

TrainUtil.CompareStrLists()

TrainUtil.GetUpTo()

TrainUtil.HasPrefix()

TrainUtil.HasSufix() (sic)

TrainUtil.StrSubst()


Categories

Personal tools