Free mag vol1 | Page 162

CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I String Member Meaning in Life PadLeft() PadRight() These methods are used to pad a string with some characters. Remove() Use these methods to receive a copy of a string with modifications (characters removed or replaced). Replace() Split() This method returns a String array containing the substrings in this instance that are delimited by elements of a specified char array or string array. Trim() This method removes all occurrences of a set of specified characters from the beginning and end of the current string. ToUpper() These methods create a copy of the current string in uppercase or lowercase format, respectively. ToLower() 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. Here is the initial output: ***** Fun with Strings ***** => Basic String functionality: Value of firstName: Freddy 96