嘿所有,我正在尝试拆分用户名,但我遇到了这样的问题。如果用户名在括号()中有“go by”名称,则下面是我的版本:
theName = "Gates, Bill W. (Bill)"
hasBracket = False
If InStr(theName, "(") <> 0 Then
hasBracket = True
End If
If hasBracket = True Then
bracketPos = InStr(theName, ",")
lName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
theName = Trim(Mid(theName, bracketPos + 2))
bracketPos = InStr(theName, " ")
fName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
theName = Trim(Mid(theName, bracketPos))
If Trim(Left(theName, 1)) <> "(" Then
mName = StrConv(Replace(Trim(Left(theName, 1)), ".", ""), vbProperCase)
Else
mName = ""
End If
bracketPos = InStr(theName, "(")
bName = StrConv(Replace(Replace(Trim(Mid(theName, bracketPos)), "(", ""), ")", ""), vbProperCase)
Else
...
End If
只要用户名中包含“()”,该部分就可以正常工作。现在进入不使用“()”的ELSE部分对我来说似乎是一个挑战。问题是用户名可以这样格式化:
Gates, Bill W.
Gates, Bill
也许我只是在想它,但我似乎无法检查它是否有中间名或不使用上面的代码为ELSE部分:
ELSE
bracketPos = InStr(theName, ",")
lName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
theName = Trim(Mid(theName, bracketPos + 2))
bracketPos = InStr(theName, " ")
If bracketPos <> 0 Then
fName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
End If
theName = Trim(Mid(theName, bracketPos))
If Trim(Left(theName, 1)) <> "(" Then
mName = StrConv(Replace(Trim(Left(theName, 1)), ".", ""), vbProperCase)
Else
mName = ""
End If
MsgBox lName & " " & fName & " " & mName
END IF
如果用户名是“Gates,Bill W.”,上面的代码就可以了。但不是如果它的“盖茨,比尔”。我该怎么做才能检查它是否有中间名,因为目前,如果没有,那我就行了错误:
theName = Trim(Mid(theName, bracketPos))
任何帮助都会很棒,谢谢! :O)
大卫
答案 0 :(得分:1)
不确定我的头脑,但这是编写一系列测试有用的地方。
我不确定VB6是否有任何类型的TDD软件,但您可以轻松制作一些东西来帮助您编写“UserNameSplit”功能
在您的方法结束时,打印出来,您的名字部分,名字,姓氏等
然后编写一个TestMethod,用一些简单的“Bill Gates”调用UserNameSplit 调用它,当打印输出正确时,再向UserNameSplit添加一个更复杂的“Gates,Bill”调用
继续逐步添加电话。确保在向UserNameSplit方法添加新功能之前不会破坏任何内容。只要其中一个输出不正确,请备份到工作状态,然后重试。
另外,查看代码需要停止重用变量。如果它被命名为“bracketPos”,它应该永远不会包含逗号或空格的位置。它使代码很难阅读和遵循,这可能是这种方法令人困惑的原因,也很难找到你的bug。
答案 1 :(得分:0)
我不知道为什么我之前没有想到这个!解决了! :O)
Dim arryTmp() As String
Dim theName As String
Dim fName As String
Dim lName As String
Dim mName As String
Dim bName As String
theName = "Gates, Bill W."
arryTmp = Split(theName, " ")
If InStr(theName, "(") <> 0 Then
If UBound(arryTmp) = 3 Then
lName = Replace(StrConv(Trim(arryTmp(0)), vbProperCase), ",", "")
fName = StrConv(Trim(arryTmp(1)), vbProperCase)
mName = Replace(StrConv(Trim(arryTmp(2)), vbProperCase), ".", "")
bName = Replace(Replace(StrConv(Trim(arryTmp(3)), vbProperCase), ")", ""), "(", "")
ElseIf UBound(arryTmp) = 2 Then
lName = Replace(StrConv(Trim(arryTmp(0)), vbProperCase), ",", "")
fName = StrConv(Trim(arryTmp(1)), vbProperCase)
mName = Replace(StrConv(Trim(arryTmp(2)), vbProperCase), ".", "")
ElseIf UBound(arryTmp) = 1 Then
lName = Replace(StrConv(Trim(arryTmp(0)), vbProperCase), ",", "")
fName = StrConv(Trim(arryTmp(1)), vbProperCase)
End If
Else
If UBound(arryTmp) = 3 Then
lName = Replace(StrConv(Trim(arryTmp(0)), vbProperCase) & " " & StrConv(Trim(arryTmp(1)), vbProperCase), ",", "")
fName = StrConv(Trim(arryTmp(2)), vbProperCase)
mName = Replace(StrConv(Trim(arryTmp(3)), vbProperCase), ".", "")
ElseIf UBound(arryTmp) = 2 Then
lName = Replace(StrConv(Trim(arryTmp(0)), vbProperCase), ",", "")
fName = StrConv(Trim(arryTmp(1)), vbProperCase)
mName = Replace(StrConv(Trim(arryTmp(2)), vbProperCase), ".", "")
ElseIf UBound(arryTmp) = 1 Then
lName = Replace(StrConv(Trim(arryTmp(0)), vbProperCase), ",", "")
fName = StrConv(Trim(arryTmp(1)), vbProperCase)
End If
End If
MsgBox lName & " " & fName & " " & mName & " " & bName
MsgBox lName & " " & fName & " " & mName & " " & bName