This documentation is archived and is not being maintained.

How to: Perform String Manipulations by Using Basic String Operations 

The following example uses some of the methods discussed in the Basic String Operations topics to construct a class that performs string manipulations in a manner that might be found in a real-world application. The MailToData class stores the name and address of an individual in separate properties and provides a way to combine the City, State, and Zip fields into a single string for display to the user. Furthermore, the class allows the user to enter the city, state, and ZIP Code information as a single string; the application automatically parses the single string and enters the proper information into the corresponding property.

For simplicity, this example uses a console application with a command-line interface.


using System;
class MainClass
    static void Main(string[] args)
        MailToData MyData = new MailToData();

        Console.Write("Enter Your Name:");
        MyData.Name = Console.ReadLine();
        Console.Write("Enter Your Address:");
        MyData.Address = Console.ReadLine();
        Console.Write("Enter Your City, State, and ZIP Code separated by spaces:");
        MyData.CityStateZip = Console.ReadLine();

        Console.WriteLine("Name: {0}", MyData.Name);
        Console.WriteLine("Address: {0}", MyData.Address);
        Console.WriteLine("City: {0}", MyData.City);
        Console.WriteLine("State: {0}", MyData.State);
        Console.WriteLine("Zip: {0}", MyData.Zip);

        Console.WriteLine("The following address will be used:");


public class MailToData
    string   name = " ";
    string   address = " "; 
    string  citystatezip = " ";
    string   city = " "; 
    string   state = " "; 
    string   zip = " ";

    public MailToData()

    public string Name
        get{return name;}
        set{name = value;}

    public string Address
        get{return address;}
        set{address = value;}

    public string CityStateZip
            return ReturnCityStateZip();
            citystatezip = value;

    public string City
        get{return city;}
        set{city = value;}

    public string State
        get{return state;}
        set{state = value;}

    public string Zip
        get{return zip;}
        set{zip = value;}

    private void ParseCityStateZip()
        int CityIndex; 
        int StateIndex;
        // Check for an exception if the user did not enter spaces between
        // the elements.

            // Find index position of the space between
            // city and state and assign that value to CityIndex.
            CityIndex = citystatezip.IndexOf(" ");

            // Initialize the CityArray to the value of the 
            // index position of the first white space.
            char[] CityArray = new char[CityIndex];

            // Copy the city to the CityArray.
            citystatezip.CopyTo(0,CityArray ,0, CityIndex);

            // Find index position of the space between
            // state and zip and assign that value to CityIndex.
            StateIndex = citystatezip.LastIndexOf(" ");
            // Initialize the StateArray to the length of the state.
            char[] StateArray = new char[StateIndex - CityIndex];

            // Copy the state to the StateArray.
            citystatezip.CopyTo(CityIndex, StateArray, 0, (StateIndex - CityIndex));

            // Initialize the ZipArray to the length of the zip.
            char[] ZipArray = new char[citystatezip.Length - StateIndex];

            // Copy the zip to the ZipArray.
            citystatezip.CopyTo(StateIndex, ZipArray, 0, (citystatezip.Length - StateIndex));
            // Assign city to the value of CityArray.
            city = new String(CityArray);

            // Trim white spaces, commas, and so on.
            city = city.Trim(new char[]{' ', ',', ';', '-', ':'}); 

            // Assign state to the value of StateArray.
            state = new String(StateArray);

            // Trim white spaces, commas, and so on.
            state = state.Trim(new char[]{' ', ',', ';', '-', ':'});

            // Assign zip to the value of ZipArray.
            zip = new String(ZipArray);

            // Trim white spaces, commas, and so on.
            zip = zip.Trim(new char[]{' ', ',', ';', '-', ':'});
        // If an exception is encountered, alert the user to enter spaces
        // between the elements.
            Console.WriteLine("\n\nYou must enter spaces between elements.\n\n");

    private string ReturnCityStateZip()
        // Make state uppercase.
        state = state.ToUpper();

        // Put the value of city, state, and zip together in the proper manner.
        string MyCityStateZip = String.Concat(city, ", ", state, " ", zip);
        return MyCityStateZip;

When the preceding code is executed, the user is asked to enter his or her name and address. The application places the information in the appropriate properties and displays the information back to the user, creating a single string that displays the city, state, and ZIP Code information.

See Also