文章編號: 168799 - 上次校閱: 2006年10月12日 - 版次: 4.3

如何將一個人名剖析為多個變數

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文提供的程序,可將包含人名的單一變數剖析為 6 個部分:
Title                (Mr., Ms., etc.)
First Name
Middle Initial(s)
Last Name
Pedigree             (Jr., Sr., III, etc.)
Degree(s)            (B.S., PhD, etc.)
				

其他相關資訊

警告:使用本文所提供之程式碼/巨集必須自行承擔風險。Microsoft 僅以「現狀」提供此程式碼/巨集,不做任何明示或默示的保證,包括 (但不限於) 其適售性和/或適合某特定用途之默示擔保。

注意:在下列範例程式碼中,行結尾的底線 (_) 是做為行接續字元。針對不支援行接續字元的 BASIC 版本,請移除行結尾的底線,並在重新建立此程式碼時合併下面的行。

欄位以下列順序剖析,並從名稱中移除:
Title - (如果第一個字符合標準 Title 的清單)
Degree - (第一個逗號後的項目)
Pedigree - (如果最後一個字符合標準 Pedigree 清單)
Last Name - (不可包含空格,但可以有連字符號)
First Name - (不可包含空格)
Middle Initial(s) - (剩下的項目)

逐步範例

  1. 請輸入下列程式碼:
          Function CutLastWord (ByVal S As String, Remainder As String) _
              As String
          ' CutLastWord: returns the last word in S.
          ' Remainder: returns the rest.
          '
          ' Words are separated by spaces
          '
          Dim  I As Integer, P As Integer
            S = Trim$(S)
            P = 1
            For I = Len(S) To 1 Step -1
              If Mid$(S, I, 1) = " " Then
                P = I + 1
                Exit For
              End If
            Next I
            If P = 1 Then
              CutLastWord = S
              Remainder = ""
            Else
              CutLastWord = Mid$(S, P)
              Remainder = Trim$(Left$(S, P - 1))
            End If
          End Function
    
          Function CutWord (ByVal S As String, Remainder As String) As String
          '
          ' CutWord: returns the first word in S.
          ' Remainder: returns the rest.
          '
          Dim P As Integer
            S = Trim$(S)
            P = InStr(S, " ")
            If P = 0 Then P = Len(S) + 1
            CutWord = Left$(S, P - 1)
            Remainder = Trim$(Mid$(S, P + 1))
          End Function
    
          Sub ParseName (ByVal S As String, Title As String, FName As String, _
                         MName As String, LName As String, _
                         Pedigree As String, Degree As String)
          Dim Word As String, P As Integer, Found As Integer
          Const Titles = "Mr.Mrs.Ms.Dr.Miss,Sir,Madam,Mayor,President"
          Const Pedigrees = "Jr.Sr.III,IV,VIII,IX,XIII"
            Title = ""
            FName = ""
            MName = ""
            LName = ""
            Pedigree = ""
            Degree = ""
          '
          ' Get Title
          '
            Word = CutWord(S, S)
            If InStr(Titles, Word) Then
              Title = Word
            Else
              S = Word & " " & S
            End If
          '
          ' Get Degree
          '
            P = InStr(S, ",")
            If P > 0 Then
              Degree = Trim$(Mid$(S, P + 1))
              S = Trim$(Left$(S, P - 1))
            End If
          '
          ' Get Pedigree
          '
            Word = CutLastWord(S, S)
            If InStr(Pedigrees, Word) Then
              Pedigree = Word
            Else
              S = S & " " & Word
            End If
          '
          ' Get the rest
          '
            LName = CutLastWord(S, S)   ' Last Name
            FName = CutWord(S, S)       ' First Name
            MName = Trim(S)             ' Initials/Middle Name(s)
          End Sub
    						
  2. 如果要進行測試,請建立一個具有七個文字方塊 (txtName、txtTitle、txtFirstName、txtMI、txtLastName、txtPedigree、txtDegree) 的表單,以及一個命令按鈕。新增下列程式碼:
          Sub Command1_Click()
          Dim Title As String, FName As String, MI As String
          Dim LName As String, Pedigree As String, Degree As String
            ParseName txtName, Title, FName, MI, LName, Pedigree, Degree
            txtTitle = Title
            txtFirstName = FName
            txtMI = MI
            txtLastName = LName
            txtPedigree = Pedigree
            txtDegree = Degree
          End Sub
    						
  3. 顯示表單、輸入一個名稱到 txtName,然後按一下命令按鈕。其他的六個欄位應該包含剖析後的值。

其他資訊

其他注意事項、限制及改進建議 (讀者裝置的左邊):

  1. 剖析常式假設 Degree 之前有逗號,但 Pedigree 之前則沒有逗號。如果 Pedigree 前有逗號,逗號將會包含在 Degree 中。如果您希望 Pedigree 前面出現逗號,可新增額外的檢查,查看 Degree 的第一個字 (逗號分隔) 是否出現在標準 Pedigrees 清單中。
  2. 第一個及最後一個名稱皆假設不包含空格。如果名稱包含空格 (如 Mary Beth Saint John),一部分的名稱 (如 Beth Saint) 會出現在 Middle Initial/Middle Name 傳回值中。可惜,因為這個情形的變異性極大,所以除了手動操作,沒有更好的解決方法。
  3. 剖析常式設計為適用於英文/美國名稱。其他語言可能有不同的慣例,可能必須調整剖析程式碼。
  4. 可在 ParseName 的兩個 Const 宣告中新增項目,輕鬆地延伸標準 Titles 及 Pedigrees 清單。
  5. 可以用一個比較一般的方式檢查 Title,那就是查看第一個字結尾的句號 (如果第一個字結尾的句號不在標準 Titles 清單中)。

這篇文章中的資訊適用於:
  • Microsoft Visual Basic Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Word 97 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
關鍵字:?
kbhowto kbprogramming KB168799
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。