我需要從數組中構建逗號分隔的字符串。從vb.net中的字符串中刪除最終的逗號
For i=0 to ubound(arr)
str+= arr(i) & ","
Next i
麻煩的是,我最後得到了一個額外的逗號。
我去除逗號電流的方法是:
If str<>"" then
Left(str, Len(str) - 1)
End if
但似乎很凌亂,和非通用。我必須經常這樣做,這會變得很痛苦。任何人都有更好的?
我需要從數組中構建逗號分隔的字符串。從vb.net中的字符串中刪除最終的逗號
For i=0 to ubound(arr)
str+= arr(i) & ","
Next i
麻煩的是,我最後得到了一個額外的逗號。
我去除逗號電流的方法是:
If str<>"" then
Left(str, Len(str) - 1)
End if
但似乎很凌亂,和非通用。我必須經常這樣做,這會變得很痛苦。任何人都有更好的?
您可以使用String.Join
。請參閱文件的位置:http://msdn.microsoft.com/en-us/library/dd988350.aspx
示例代碼:
Dim values() As Object = {"Cobb", 4189, 11434, .366 }
Console.WriteLine(String.Join(",", values))
' The example displays the following output:
' Cobb,4189,11434,0.366
輝煌。那很完美。 – Urbycoz 2011-02-25 11:53:52
假定您已經在陣列中的至少一個項目:
str += arr(0)
For i=1 to ubound(arr)
str+= "," & arr(i)
Next i
真正的答案是'String.Join',但在從字符串看的結尾處,刪除一個或多個特定的字符[' String.TrimEnd'](http://msdn.microsoft.com/en-us/library/system.string.trimend.aspx)。 – Richard 2011-02-25 11:45:38