Game Development Reference
In-Depth Information
Although String.Compare returns 0 to indicate that two strings are
equal, never use this function for equality testing. For equality testing,
use String.Equals or hashes, as both perform much faster than
String.Compare .
String formatting
If you're creating GUI elements, such as high-score HUDs, player names, cash
counters, or resources indicators, you'll not only need to show literal text but also
numerical values inside the strings, for example, by combing the word Score: with
a string representation of the actual score, which will change over time depending
on player performance. One way to achieve this is the String.Format method,
as shown in the following code sample 6-15:
//Construct string from three numbers
public void BuildString(int Num1, int Num2, float Num3)
{
string Output = string.Format("Number 1 is: {0}, Number 2 is:
{1}, Number 3 is: {2}", Num1, Num2, Num3);
Debug.Log (Output.ToString("n2"));
}
String looping
So far, we've seen IEnumerable and IEnumerator . Thankfully, these interfaces apply
to strings and can be used to loop or cycle through every letter in a string. This can
be achieved using either the IEnumerator interface itself or via a foreach loop.
Let's see both ways, as shown in the following code sample 6-16 and 6-17:
//Sample 6-16
//Loops through string in foreach
public void LoopLettersForEach(string Str)
{
//For each letter
foreach(char C in Str)
{
//Print letter to console
Debug.Log (C);
 
Search WWH ::




Custom Search