开发者

Splitting strings in VB.Net

开发者 https://www.devze.com 2023-01-15 15:06 出处:网络
I开发者_运维百科 have this code: Dim StringParts As New List(Of String)(OriginalString.Split(New Char() {\"\\\"c}, StringSplitOptions.RemoveEmptyEntries))

I开发者_运维百科 have this code:

Dim StringParts As New List(Of String)(OriginalString.Split(New Char() {"\"c}, StringSplitOptions.RemoveEmptyEntries))

When run, StringParts always have one element, because StringSplitOptions.RemoveEmptyEntries = 1.

How can I tell VB.Net to use the right function, and not understand StringSplitOptions.RemoveEmptyEntries as a count parameter?

Thanks!

Note: Using New String() {"\"} instead of New Char() {"\"c} works. Is this a .Net bug?


Unable to reproduce:

Imports System
Imports System.Collections.Generic

Public Class Test

    Public Shared Sub Main()
      Dim originalString As String = "a\b\c"
      Dim stringParts As New List(Of String)( _
          OriginalString.Split(New Char() {"\"c}, _
          StringSplitOptions.RemoveEmptyEntries))
      Console.WriteLine(stringParts.Count)
    End Sub    

End Class

The above code prints 3 (compiled with both VS2010 and VS2008, to avoid this being a missing overload issue). In both cases the compiled code is using the enum value appropriately, rather than converting it into a number.

I suspect your problem lies elsewhere. Please try to edit your question with a short but complete program which demonstrates the problem.


I don't see where the problem is:

Dim OriginalString = "part1\part2\part3"
Dim StringParts As New List(Of String)( _
    OriginalString.Split( _
        New Char() {"\"}, _
    StringSplitOptions.RemoveEmptyEntries _
    ) _
)

works as expected. StringParts contains 3 elements.

0

精彩评论

暂无评论...
验证码 换一张
取 消