防止`vbLf`包含在`String.Length`计算中

时间:2014-01-09 15:54:34

标签: vb.net text split trim

我有一个Collection个服务器,我从多行文本框中获取。

我有一些简单的验证应该修剪空格(以防止为空行创建条目),但它不起作用。

例如,如果Me.formServers.txtServers.Text如下所示,则行的长度将返回为5,5,5和4.如何正确计算每行的长度,从而避免添加错误的项目我的Collection?感谢。

  

TTSA
  TTSB
  TTSC
  TTSD

这是我的代码

Me.Servers = New Collection ' Reset 'Servers' to ensure only the correct servers are included

For Each Server As String In Me.formServers.txtServers.Text.Split(vbLf)
    If Not Server.Trim.Length = 0 Then Me.Servers.Add(Server)
    MsgBox(Server.Length)
Next

1 个答案:

答案 0 :(得分:0)

这个测试很有意思:

    Dim s As String = "TTSA" & vbCrLf
    s &= "TTSB" & vbLf
    s &= "TTSC" & vbCr
    s &= "TTSD" & Environment.NewLine
    s &= "TTSE" & vbNewLine

    Dim Excluded() As String
    Excluded = s.Split({vbCrLf, vbLf}, StringSplitOptions.None)
    For Each s In Excluded
        Debug.Print(s & " " & s.Length)
    Next

    result:
    TTSA 4
    TTSB 4
    TTSC     ' vbCr was not in list so is still in the string
    TTSD 9
    TTSE 4
     0       ' last separator honored

     Corrected to:    
        Excluded = s.Split({vbCrLf, vbLf, vbCr}, _
                       StringSplitOptions.RemoveEmptyEntries)