我在这里有一些我已经帮助修改的代码,但是这段代码中有一部分可以更简化( if orelse 语句)。我知道它与集合类或数组有关,但我对如何更改它感到困惑。任何帮助将不胜感激。
Function GetLastName(ByVal accountName As String) As String
Dim lastName As
Dim stringArray As Array = accountName.Split(" ")
Dim lastIndex = stringArray.Length - 1 'lastIndex will serve as the UpperBound of the Array
Dim lastElement = stringArray(lastIndex).ToUpper
If ((lastElement.Equals("JR")) OrElse (lastElement.Equals("JR.")) _
OrElse (lastElement.Equals("SR")) OrElse (lastElement.Equals("SR.")) _
OrElse (lastElement.Equals("I")) OrElse (lastElement.Equals("II")) _
OrElse (lastElement.Equals("III")) OrElse (lastElement.Equals("IV")) _
OrElse (lastElement.Equals("M.D.")) OrElse (lastElement.Equals("M.D")) _
OrElse (lastElement.Equals("PH.D.")) OrElse (lastElement.Equals("PHD.")) _
OrElse (lastElement.Equals("PHARM.D")) OrElse (lastElement.Equals("M.B.A"))) Then
答案 0 :(得分:3)
Dim titles As IList(Of String) = New List(Of String)()
titles.Add("JR")
titles.Add("JR.")
titles.Add("SR")
titles.Add("SR.")
' ....
Return titles.Contains(lastElement)