Team LiB
Previous Section Next Section

Character Manipulation

Along with working with substrings, you can also manipulate characters within the string. You can easily convert the case of characters, replace characters, and remove characters.

Changing Character Case

The string class provides two methods that allow you to change an existing string into all uppercase or all lowercase characters:

  • The ToUpper method changes an existing string to all uppercase characters.

  • The ToLower method changes a string to all lowercase characters.

The following example demonstrates using these methods to change character case.

Code Example: Changing Character Case
Start example
using System;

namespace Client.Chapter_6___Strings
{
      class ChangingCharacters
      {
            static void Main(string[] args)
            {
                  string MyString = "Miami, Dolphins";

                  Console.WriteLine(MyString);
                 //Converts all characters to uppercase
                  MyString.ToUpper();
                  Console.WriteLine(MyString);
                 //Converts all characters to lowercase
                  MyString.ToLower();
                  Console.WriteLine(MyString);
            }
      }
}
End example

Replacing Characters

You easily replace characters in a StringBuilder class by using the Replace method. The first parameter is the character you would like to replace, and the second parameter is the replacement character. The string will then be searched for all instances of that character, and that character will be replaced with the one you specified.

The following example demonstrates how to replace characters in a string.

Code Example: Replacing Characters
Start example
using System;
using System.Text;

namespace Client.Chapter_6___Strings
{
      class ReplacingCharacters
      {
            static void Main(string[] args)
            {
                  StringBuilder MyString = new StringBuilder("AAAAABBB");

                  Console.WriteLine(MyString);
                 //Replaces all the As with Fs
                  MyString.Replace("A", "F");
                  Console.WriteLine(MyString);
            }
      }
}
End example

Removing Characters

The Remove method of the string class allows you to remove characters from a string. It takes two parameters. The first parameter is the zero-based index into the string, specifying where you wish to start. The second parameter is the count of the number of characters you would like to remove.

The following example removes characters from an existing string, printing a string that says "Hello World."

Code Example: Removing Characters
Start example
Using System;

namespace Client.Chapter_6___Strings
{
      class RemovingCharacters
      {
            static void Main(string[] args)
            {
                  string MyString = "Hello UnderWorld";

                  Console.WriteLine(MyString.Remove(7, 5));
            }
      }
}
End example

Team LiB
Previous Section Next Section