Warm tip: This article is reproduced from serverfault.com, please click
vb6

其他-如何在 vb6 中将全名大写

(其他 - How capitalize fullname in vb6)

发布于 2017-08-12 16:07:39

大家好,我有这个问题,如下所示,如何在一个 vb6 Vb6 字符串变量 'example 'my fullname Dim fullname as string Fullname = “abdirahman abdirisaq ali” Msgbox capitalize(fullname) 它打印 abdirahmanAbdirisaq ali 这意味着它跳过中间名称空间即使我增加了同样的空间。这是我自己的代码和努力,它至少需要我 2 个小时,而且仍然如此。我累了累了累了请救救我多谢。请检查我的代码并帮助我我写的错误类型是什么。这是我的代码

                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
Questioner
Abdirahman Tusbahle
Viewed
11
thx1138v2 2017-08-13 08:25:40

它比那简单得多。在 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

这是相当容易的部分。如果你只打算处理人名,那么处理 MacFarland、O'Connor 等名称仍需要更多工作。

企业名称变得更加复杂,因为它们可以有一个像“湖上村庄公寓”这样的名字,其中一些单词没有大写。这是一个合法的商业名称,所以大小写很重要。

如果所有内容都为小写,Professional 和 business 后缀也可能会出现问题——比如 phd 应该是 PhD,llc 应该是 LLC,而 iii,就像在 John Smith III 中一样,会出现 Iii。

还有一个 VB6 函数可以将每个单词的第一个字母大写。它是 StrConv(string,vbProperCase) 但它也将不是第一个字母的所有内容设置为小写。所以PhD变成Phd,III变成III。由于上面的代码没有将尾随部分更改为小写,因此如果输入正确,它仍然是正确的。