Strings

From ETC Public Wiki
Jump to: navigation, search

Declare a variable of type string:

var name : String = "d10";


Find the character at an index:

print(name[0]);

var suit : String = name[0];


Remove a portion of the string - in this case the first letter

print(name.Remove(0,1));


Convert the number portion into an int

var num : int = int.Parse(name.Remove(0,1));


Insert a character into the string at index 0

name = name.Insert(0,"c");


Removing chars from a String:

string.Remove(index); // removes all chars from index to the end

string.Remove(index,int); // removes int chars starting at index


splitting a string into an array of strings

 var combined = "one,two,three";
 var words = combined.Split(","[0]);
 print( words[0] );

the function is expecting a Char so the [0] is telling javascript to take the 0th char of the string ","


if the split identifier needs to be more then a single char then use this:

 var test = "1a11abc2b22abc3c33";
 var strings = test.Split(["abc"], System.StringSplitOptions.None);
 for (s in strings) print (s);


Remove all leading and trailing white space:

string.Trim();

Format a string:

print("time = " + String.Format("{0:00}:{1:00}",minutes,seconds)); // where the 0 & 1 is the variable number and the 00's are the number of digits

or

print("time = " + String.Format("{0:D2}:{1:D2}",minutes,seconds));