Basic String Manipulation
Working with the members of System.String is as you would expect. Simply declare a string variable
and make use of the provided functionality via the dot operator. Be aware that a few of the members of
System.String are static members and are, therefore, called at the class (rather than the object) level.
Assume you have created a new Console Application project named FunWithStrings. Author the
following method, which should be called from within Main():
static void BasicStringFunctionality()
{
Console.WriteLine("=> Basic String functionality:");
string firstName = "Freddy";
Console.WriteLine("Value of firstName: {0}", firstName);
Console.WriteLine("firstName has {0} characters.", firstName.Length);
Console.WriteLine("firstName in uppercase: {0}", firstName.ToUpper());
Console.WriteLine("firstName in lowercase: {0}", firstName.ToLower());
Console.WriteLine("firstName contains the letter y?: {0}",
firstName.Contains("y"));
Console.WriteLine("firstName after replace: {0}", firstName.Replace("dy", ""));
Console.WriteLine();
}
Not too much to say here, as this method simply invokes various members, such as ToUpper() and
Contains(), on a local string variable to yield various formats and transformations.
No comments:
Post a Comment