string 在VB.NET中修剪字符串的最后一个“,”分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2724487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:43:13  来源:igfitidea点击:

Trim last "," delimiter of a string in VB.NET

vb.netstringtrim

提问by gerfe

This is my code:

这是我的代码:

With ad.Tables(2)
    For i As Integer = 0 To .Rows.Count - 1
        If .Rows(i)("name") & "" <> "" Then
            temp &= .Rows(i)("name") & ", "
        End If
    Next
End With
temp = temp.Trim(",")
testing &= "&Name=" & temp & vbCrLf

With this is get a comma in the end of the string. But if I do

有了这个,在字符串的末尾得到一个逗号。但如果我这样做

temp = temp.Trim.Trim(",")

all commas are deleted.

删除所有逗号。

How do I keep all commas and only delete the last one?

如何保留所有逗号并只删除最后一个?

回答by Captain America

temp = temp.TrimEnd(CChar(","))

That will do it and I think it is the easiest way.

这样就可以了,我认为这是最简单的方法。

回答by Hans Olsson

temp = temp.Trim().Substring(0, temp.Length - 1)

or

或者

temp = temp.Trim().Remove(temp.Length - 1)

回答by Alex K.

You can avoid the Trim/extra character if you set a delimiter within the loop

如果在循环中设置分隔符,则可以避免使用 Trim/extra 字符

Dim delimiter as string = ""
For i As Integer = 0 To .Rows.Count - 1
   If .Rows(i)("name") & "" <> "" Then
      temp &= delimiter & .Rows(i)("name")
      delimiter = ","
   End If
Next

回答by SsJVasto

The Trim()function has a Char()(static array of characters) parameter on it, you don't need to pass a Charexplicitly.

Trim()函数有一个Char()(静态字符数组)参数,您不需要Char显式传递 a 。

' VB.Net Version
", Hello ^_^ ^_^ ,,, , ,,   ,, ,".Trim({" "c, ","c})

//C# version
", Hello ^_^ ^_^ ,,, , ,,   ,, ,".Trim({' ', ','})

Would produce the output

会产生输出

"Hello ^_^ ^_^"

The multi-parameter .Trim()removes the specified characters from both the beginning and the end of the string. If you want to only trim out the beginning or the end, use .TrimStart()or .TrimEnd()respectively.

多参数.Trim()从字符串的开头和结尾删除指定的字符。如果您只想剪掉开头或结尾,请分别使用.TrimStart().TrimEnd()

回答by One Day

This works:

这有效:

dim AlarmStr as string="aaa , bbb , ccc ,  "
AlarmStr = AlarmStr.Remove(AlarmStr.LastIndexOf(","))

回答by Robert Barrueco

Check to see if the loop is done before adding the last comma. #cleaner

在添加最后一个逗号之前检查循环是否完成。#清洁工

While dr.Read
   For c As Integer = 0 To dr.FieldCount - 1
       s.Append(dr(c).ToString.Trim)

       'This line checks if the loop is ending before adding a comma           
       If (c < dr.FieldCount - 1) Then s.Append(",")

   Next
   s.Append(vbCrLf)
End While

回答by Fabio

I wonder, that based on the OP's code sample, nobody suggested to use String.Joinor build an output with StringBuilder

我想知道,基于 OP 的代码示例,没有人建议使用String.Join或构建输出StringBuilder

Dim names As New List(Of String)
With ad.Tables(2)
    For i As Integer = 0 To .Rows.Count - 1
        ' DataRow("columnName").ToString() returns value or empty string if value is DbNull
        If .Rows(i)("name").ToString() <> "" Then
           names.Add(.Rows(i)("name"))
        End If
    Next
End With

Dim temp As String = String.Join(", ", names)
testing &= "&Name=" & temp & vbCrLf

With LINQ and DataRowextension method .Field(Of T)code will look simpler

使用 LINQ 和DataRow扩展方法.Field(Of T)代码看起来更简单

Dim names = ad.Tables(2).
    AsEnumerable().
    Select(row => row.Field<String>("name")).
    Where(name => String.IsNullOrEmpty(name) = False)

Dim temp As String = String.Join(", ", names)
testing &= "&Name=" & temp & vbCrLf

If you go further you can(should) use StringBuilderfor building a string and use Aggregatemethod to build string from the collection

如果您更进一步,您可以(应该)StringBuilder用于构建字符串并使用Aggregate方法从集合中构建字符串

回答by nghiavt

I know that a little bit odd, but I found this works for me:

我知道这有点奇怪,但我发现这对我有用:

Visual Basic:

视觉基础:

Every time: mystring = "today, I love go out , , , ," (less than five commas and spaces)

每次:mystring = "今天,我爱出去 , , , ," (少于五个逗号和空格)

mystring.Trim.TrimEnd(",").Trim.TrimEnd(",").Trim.TrimEnd(",").Trim.TrimEnd(",").Trim.TrimEnd(",")

回答by shradha

temp = temp.TrimEnd()

this trims the trailing spaces from a string.

这会修剪字符串的尾随空格。