我有一个数组如下:
If iAdvanceMonthNum = 1 Then
iAdvanceMonthName(1) = "January"
ElseIf iAdvanceMonthNum = 2 Then
iAdvanceMonthName(2) = "February"
ElseIf iAdvanceMonthNum = 3 Then
iAdvanceMonthName(3) = "March"
ElseIf iAdvanceMonthNum = 4 Then
iAdvanceMonthName(4) = "April"
ElseIf iAdvanceMonthNum = 5 Then
iAdvanceMonthName(5) = "May"
ElseIf iAdvanceMonthNum = 6 Then
iAdvanceMonthName(6) = "June"
ElseIf iAdvanceMonthNum = 7 Then
iAdvanceMonthName(7) = "July"
ElseIf iAdvanceMonthNum = 8 Then
iAdvanceMonthName(8) = "August"
ElseIf iAdvanceMonthNum = 9 Then
iAdvanceMonthName(9) = "September"
ElseIf iAdvanceMonthNum = 10 Then
iAdvanceMonthName(10) = "October"
ElseIf iAdvanceMonthNum = 11 Then
iAdvanceMonthName(11) = "November"
ElseIf iAdvanceMonthNum = 12 Then
iAdvanceMonthName(12) = "December"
End If
假设数组将是: iAdvanceMonth(9)=“九月”和 iAdvanceMonth(10)=“十月”
而另一个数组将为null。我想要实现的是我想要显示“九月”和“十月”。
显示我到目前为止所做的字符串的代码:
Dim display As String = String.Empty
For i As Integer = 1 To iAdvanceMonthName.Length - 1
If Not String.IsNullOrEmpty(iAdvanceMonthName(i)) Then
display = iAdvanceMonthName(i)
End If
Next
但是,输出只会是“十月”,因为代码将采用最新的数组。我可以获得一些关于如何解决这个问题的提示或技巧。非常感谢。
答案 0 :(得分:0)
试试这段代码
Dim display As String = String.Empty
For i As Integer = 1 To iAdvanceMonthName.Length - 1
If Not String.IsNullOrEmpty(iAdvanceMonthName(i)) Then
display = display & iAdvanceMonthName(i) & " And "
End If
Next
If Not String.IsNullOrEmpty(display) Then display = Left(display, display.Length - 5)
答案 1 :(得分:0)
检查此代码:
Framework 4.0 +
Module Module1
Sub Main()
Dim months As String() = {Nothing, "August", "September", ""}
Console.WriteLine(String.Join(" and ", months.Where(Function(s) Not String.IsNullOrEmpty(s))))
Console.ReadLine()
End Sub
End Module
修改强>
Framework 4.0 -
Module Module1
Sub Main()
Dim months As String() = {Nothing, "August", "September", ""}
Console.WriteLine(String.Join(" and ", months.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray))
Console.ReadLine()
End Sub
End Module