Game Development Reference
In-Depth Information
}
}
//Sample 6-17
//Loop through string as iterator
public void LoopLettersEnumerator(string Str)
{
//Get Enumerator
IEnumerator StrEnum = Str.GetEnumerator();
//Move to nextletter
while(StrEnum.MoveNext())
{
Debug.Log ((char)StrEnum.Current);
}
}
Creating strings
To make your code read better, work in a cleaner way, and generally, be more
consistent with .NET and the way it's intended to be used. It's a good practice to
avoid initializing string variables as: string MyString = ""; . Instead, try the
following code for string declaration and assignment using String.empty :
string MyString = string.Empty;
Searching strings
If you're dealing with multiple lines of text read from a file, such as a Text Asset,
you might need to find the first occurrence of a smaller string inside the larger one,
for example, finding a smaller and separate word within the larger string. You can
achieve this using the String.IndexOf method. If a match is found, the function
would return a positive integer that indicates the position in the larger string of the
first character of the found word as a measured offset from the first letter. If no match
is found, the function returns -1 , as shown in the following code sample 6-18:
//Searches string for a specified word and returns found index
of first occurrence
public int SearchString(string LargerStr, string SearchStr)
{
//Ignore case
return LargerStr.IndexOf(SearchStr,
System.StringComparison.CurrentCultureIgnoreCase);
}
 
Search WWH ::




Custom Search