visual studio 拆分功能问题

visual studio split function issue

我尝试在 visual studio 上调用拆分函数,如下所示,我希望拆分后 return 我在数组中有 2 个项目,但是 vb return 5 个结果来自我的编码。是 vb 问题还是我的编码问题?

整个字符串是 "NAME":"ALICE"

Dim a As String = """NAME"":""ALICE""" Dim b() As String = a.Split(""":")

拆分后数组中预期的输出
(1) “姓名
(2) "ALICE"

    Dim a As String = """NAME"":""ALICE"""
    Dim b() As String = a.Split(":")

这是它的评估方式

You were using this overload of String.Split(Char[]). Note that takes an array of characters. String is convertible to an array of characters (and that's why you could compile) but it is not equal. Try putting Option Strict On at the top of your code. It won't compile as you have it anymore :)

When passing a single string, each character in the string is used to split. Including each " in your argument, ":. It will split on " and :. You can get around it by passing a string array to Split using this overload of String.Split(String[], SplitStringOptions). Pass a single element array like this

Dim b = a.Split({""":"}, StringSplitOptions.RemoveEmptyEntries)

Yes, that is exactly as you said,

  • "NAME
  • "ALICE"

Do you want to get rid of the quotes in the result? You can do this

Dim b = a.Split({":", """"}, StringSplitOptions.RemoveEmptyEntries)

Then it's this,

  • NAME
  • ALICE