共用方式為


如何:在 .NET 中執行基本字串操作

下列範例會使用基本字串作業主題中所討論的一些方法,以一種可能在實際應用程式中發現的方式,來建構可執行字串操作的類別。 MailToData 類別會將個人的姓名和地址儲存在不同的屬性中,並允許您將 CityStateZip 欄位組合成單一字串,以便顯示給使用者看。 此外,類別可讓使用者以單一字串的形式輸入城市、省/市和郵遞區號資訊。 應用程式會自動剖析單一字串,並將適當的資訊輸入到對應的屬性。

為了簡單起見,這個範例會使用具有命令列介面的主控台應用程式。

範例

using System;

class MainClass
{
   static void Main()
   {
      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();

      if (MyData.Validated) {
         Console.WriteLine($"Name: {MyData.Name}");
         Console.WriteLine($"Address: {MyData.Address}");
         Console.WriteLine($"City: {MyData.City}");
         Console.WriteLine($"State: {MyData.State}");
         Console.WriteLine($"Zip: {MyData.Zip}");

         Console.WriteLine("\nThe following address will be used:");
         Console.WriteLine(MyData.Address);
         Console.WriteLine(MyData.CityStateZip);
      }
   }
}

public class MailToData
{
   string name = "";
   string address = "";
   string citystatezip = "";
   string city = "";
   string state = "";
   string zip = "";
   bool parseSucceeded = false;

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

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

   public string CityStateZip
   {
      get {
         return String.Format("{0}, {1} {2}", city, state, zip);
      }
      set {
         citystatezip = value.Trim();
         ParseCityStateZip();
      }
   }

   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;}
   }

   public bool Validated
   {
      get { return parseSucceeded; }
   }

   private void ParseCityStateZip()
   {
      string msg = "";
      const string msgEnd = "\nYou must enter spaces between city, state, and zip code.\n";

      // Throw a FormatException if the user did not enter the necessary spaces
      // between elements.
      try
      {
         // City may consist of multiple words, so we'll have to parse the
         // string from right to left starting with the zip code.
         int zipIndex = citystatezip.LastIndexOf(" ");
         if (zipIndex == -1) {
            msg = "\nCannot identify a zip code." + msgEnd;
            throw new FormatException(msg);
         }
         zip = citystatezip.Substring(zipIndex + 1);

         int stateIndex = citystatezip.LastIndexOf(" ", zipIndex - 1);
         if (stateIndex == -1) {
            msg = "\nCannot identify a state." + msgEnd;
            throw new FormatException(msg);
         }
         state = citystatezip.Substring(stateIndex + 1, zipIndex - stateIndex - 1);
         state = state.ToUpper();

         city = citystatezip.Substring(0, stateIndex);
         if (city.Length == 0) {
            msg = "\nCannot identify a city." + msgEnd;
            throw new FormatException(msg);
         }
         parseSucceeded = true;
      }
      catch (FormatException ex)
      {
         Console.WriteLine(ex.Message);
      }
   }

   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;
    }
}
Class MainClass
    Public Shared Sub Main()
        Dim MyData As 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()

        If MyData.Validated Then
            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 Code: {0}", MyData.Zip)

            Console.WriteLine("The following address will be used:")
            Console.WriteLine(MyData.Address)
            Console.WriteLine(MyData.CityStateZip)
        End If
    End Sub
End Class

Public Class MailToData
    Private strName As String = ""
    Private strAddress As String = ""
    Private strCityStateZip As String = ""
    Private strCity As String = ""
    Private strState As String = ""
    Private strZip As String = ""
    Private parseSucceeded As Boolean = False

    Public Property Name() As String
        Get
            Return strName
        End Get
        Set
            strName = value
        End Set
    End Property

    Public Property Address() As String
        Get
            Return strAddress
        End Get
        Set
            strAddress = value
        End Set
    End Property

    Public Property CityStateZip() As String
        Get
            Return String.Format("{0}, {1} {2}", strCity, strState, strZip)
        End Get
        Set
            strCityStateZip = value.Trim()
            ParseCityStateZip()
        End Set
    End Property

    Public Property City() As String
        Get
            Return strCity
        End Get
        Set
            strCity = value
        End Set
    End Property

    Public Property State() As String
        Get
            Return strState
        End Get
        Set
            strState = value
        End Set
    End Property

    Public Property Zip() As String
        Get
            Return strZip
        End Get
        Set
            strZip = value
        End Set
    End Property

    Public ReadOnly Property Validated As Boolean
        Get
            Return parseSucceeded
        End Get
    End Property

    Private Sub ParseCityStateZip()
        Dim msg As String = Nothing
        Const msgEnd As String = vbCrLf +
                                 "You must enter spaces between city, state, and zip code." +
                                 vbCrLf

        ' Throw a FormatException if the user did not enter the necessary spaces
        ' between elements. 
        Try
            ' City may consist of multiple words, so we'll have to parse the 
            ' string from right to left starting with the zip code.
            Dim zipIndex As Integer = strCityStateZip.LastIndexOf(" ")
            If zipIndex = -1 Then
                msg = vbCrLf + "Cannot identify a zip code." + msgEnd
                Throw New FormatException(msg)
            End If
            strZip = strCityStateZip.Substring(zipIndex + 1)

            Dim stateIndex As Integer = strCityStateZip.LastIndexOf(" ", zipIndex - 1)
            If stateIndex = -1 Then
                msg = vbCrLf + "Cannot identify a state." + msgEnd
                Throw New FormatException(msg)
            End If
            strState = strCityStateZip.Substring(stateIndex + 1, zipIndex - stateIndex - 1)
            strState = strState.ToUpper()

            strCity = strCityStateZip.Substring(0, stateIndex)
            If strCity.Length = 0 Then
                msg = vbCrLf + "Cannot identify a city." + msgEnd
                Throw New FormatException(msg)
            End If
            parseSucceeded = True
        Catch ex As FormatException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class

執行上述程式碼時,會要求使用者輸入自己的姓名和地址。 應用程式會將這項資訊放入適當的屬性中,並建立出顯示城市、省/市和郵遞區號資訊的單一字串,將資訊重新顯示給使用者看。

另請參閱