如何在VB6利用全称

问题描述:

大家好我有这个问题,因为波纹管 如何把握全在一个VB6 VB6字符串变量 “例如 ”我的全名 昏暗的全名作为字符串 全名=‘abdirahman abdirisaq阿里’ MSGBOX利用(全名) 它打印abdirahmanAbdirisaq阿里这意味着它跳过中间名称空间,即使我增加更多的空间相同。 这是我自己的代码和努力,它需要我至少2个小时,仍然。 我累了累累,请救我多谢。 请检查我的代码并帮助我了解我写的错误类型。 这是我的代码如何在VB6利用全称

   Private Function capitalize(txt As String) As String 
         txt = LTrim(txt) 
         temp_str = "" 
         Start_From = 1 
         spacing = 0 
          For i = 1 To Len(txt) 

          If i = 1 Then 
          temp_str = UCase(Left(txt, i)) 
          Else 
         Start_From = Start_From + 1 
          If Mid(txt, i, 1) = " " Then 
           Start_From = i 
           spacing = spacing + 1 
          temp_str = temp_str & UCase(Mid(txt, Start_From + 1, 1)) 
          Start_From = Start_From + 1 
          Else 
          temp_str = temp_str & LCase(Mid(txt, Start_From, 1)) 
          End If 
          End If 
          Next i 
          checkName = temp_str 
       End Function 

它比这更简单。在VB6中,您应该使用Option Explicit正确键入您的变量。这也需要你声明它们。

Option Explicit 
Private Function capitalize(txt As String) As String 
    Dim temp_str as String 
    Dim Names As Variant 
    Dim Index As Long 

    'Remove leading and trailing spaces 
    temp_str = Trim$(txt) 

    'Remove any duplicate spaces just to be sure. 
    Do While Instr(temp_str, " ") > 0 
     temp_str = Replace(temp_str, " ", " ") 
    Loop 

    'Create an array of the individual names, separating them by the space delimiter 
    Names = Split(temp_str, " ") 

    'Now put them, back together with capitalisation 
    temp_str = vbnullstring 
    For Index = 0 to Ubound(Names) 
     temp_str = temp_str + Ucase$(Left$(Names(Index),1)) + Mid$(Names(Index),2) + " " 
    Next 
    'Remove trailing space 
    capitalize = Left$(temp_str, Len(temp_str) - 1) 
End Function 

这是相当容易的部分。如果你只是要处理人的名字,它仍然需要更多的工作来处理名称,如麦克法兰,奥康纳等。

商业名称变得更加复杂,因为他们可以有一个名称,如“湖上公寓”有些词不用大写。这是一个合法的商业名称,所以大写字母很重要。

如果一切都是小写的,专业和业务后缀也可能有问题 - 比如phd应该是博士,llc应该是LLC,并且iii,就像约翰史密斯三世会出来的那样。

还有一个VB6函数将大写每个单词的第一个字母。它是StrConv(字符串,vbProperCase),但它也将不是第一个字母的所有内容设置为小写。所以博士成为Phd,III成为ii。由于上述代码不会将尾部更改为小写,所以如果输入正确,它仍然是正确的。

+0

感谢,这是我想要的东西..感谢hx1138v2 –

+0

它的工作原理MeFine –

试试这个

Option Explicit 

Private Sub Form_Load() 

    MsgBox capitalize("abdirahman  abdirisaq  ali") 
    MsgBox capitalize("abdirahman abdirisaq ali") 

End Sub 

Private Function capitalize(txt As String) As String 

    Dim Names() As String 
    Dim NewNames() As String 
    Dim i As Integer 
    Dim j As Integer 

    Names = Split(txt, " ") 
    j = 0 
    For i = 0 To UBound(Names) 
     If Names(i) <> "" Then 
      Mid(Names(i), 1, 1) = UCase(Left(Names(i), 1)) 
      ReDim Preserve NewNames(j) 
      NewNames(j) = Names(i) 
      j = j + 1 
     End If 
    Next 

    capitalize = Join(NewNames, " ") 

End Function 
+0

马丽娟感谢。如何在大写之前检查每个单词是否是字符串。有人可以写“.abdirahman .abdrisaq .ali”。 ? –