从 vb.net 中的字符串中删除最后一个逗号 [英] Remove final comma from string in vb.net

查看:81
本文介绍了从 vb.net 中的字符串中删除最后一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数组构建一个逗号分隔的字符串.

I need to build a comma-delimited string from an array.

For i=0 to ubound(arr)
    str+= arr(i) & ","
Next i

问题是我最后多了一个逗号.

The trouble is that I end up with an extra comma at the end.

我目前删除逗号的方法是:

My current method for removing the comma is:

If str<>"" then
    Left(str, Len(str) - 1)
End if

但它看起来很不整洁,而且不通用.我必须经常这样做,这变得很痛苦.有人有更好的吗?

But it seems pretty untidy, and non-generic. I have to do this so often, it's become a pain. Anyone got a better one?

推荐答案

您可以使用 String.Join.请参阅此处的文档:http://msdn.microsoft.com/en-us/library/dd988350.aspx

You can use String.Join. Refer to the documentation here: 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

这篇关于从 vb.net 中的字符串中删除最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆