Class Str

From TrainzOnline
(Difference between revisions)
Jump to: navigation, search
(Categories)
 
(Find)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
*[[TrainzScript Langage Reference|API Hierarchy]]
+
*[[TrainzScript Language Reference|API Hierarchy]]
 
**Str
 
**Str
 
<br>
 
<br>
Line 17: Line 17:
 
;Syntax
 
;Syntax
 
  string s = "Afternoon";
 
  string s = "Afternoon";
  Str.TrimLeft(s,5);        // s will now be equal to "After"
+
  Str.Left(s,5);        // s will now be equal to "After"
 
;Notes
 
;Notes
 
*Sets ''s'' to be a substring of itself consisting of its first ''count'' characters.
 
*Sets ''s'' to be a substring of itself consisting of its first ''count'' characters.
Line 37: Line 37:
 
*Sets ''s'' to be a substring of itself ''count'' characters long starting with the ''first'' character.
 
*Sets ''s'' to be a substring of itself ''count'' characters long starting with the ''first'' character.
 
*Method does not return a value.
 
*Method does not return a value.
 +
<br>
 +
<I>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. </I>
 
<br>
 
<br>
  
Line 52: Line 54:
 
*Sets ''s'' to be a substring of itself composed of the last ''count'' characters.
 
*Sets ''s'' to be a substring of itself composed of the last ''count'' characters.
 
*Method does not return a value.
 
*Method does not return a value.
 +
<br>
 +
 +
==Find==
 +
{{MethodHeader|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.
 
<br>
 
<br>
  
Line 164: Line 180:
 
*First integer value in ''s''.
 
*First integer value in ''s''.
 
;Syntax
 
;Syntax
  int n = UnpackInt("21st May 2008");          // returns 21 and amends ''s'' to "st May 2008"  
+
  int n = Str.UnpackInt("21st May 2008");          // returns 21 and amends ''s'' to "st May 2008"  
 
;Notes
 
;Notes
 
*Strips the first integer value found from the string parameter and returns it in the method result.
 
*Strips the first integer value found from the string parameter and returns it in the method result.
Line 176: Line 192:
 
*First float value in ''s''.
 
*First float value in ''s''.
 
;Syntax
 
;Syntax
  float distance = UnpackFloat("21.5 miles");  // returns 21.5 and amends ''s'' to " miles".
+
  float distance = Str.UnpackFloat("21.5 miles");  // returns 21.5 and amends ''s'' to " miles".
 
;Notes
 
;Notes
 
*Strips the first floating point value found from the string parameter and returns it in the method result.
 
*Strips the first floating point value found from the string parameter and returns it in the method result.
Line 197: Line 213:
 
*Any leading white space is removed from the string before parsing.
 
*Any leading white space is removed from the string before parsing.
 
<br>
 
<br>
 +
 +
==CloneString==
 +
{{MethodHeader|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==
 
==Related Methods==

Latest revision as of 09:24, 31 January 2024


  • 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

[edit] 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.


[edit] 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.

[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] 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.


[edit] Related Methods

TrainUtil.AlreadyThereStr()

TrainUtil.CompareStrLists()

TrainUtil.GetUpTo()

TrainUtil.HasPrefix()

TrainUtil.HasSufix() (sic)

TrainUtil.StrSubst()


[edit] Categories

Personal tools