VB.net - 如何使用“分割字符串”后跟一个空格作为分隔符

发布于 2024-12-26 11:17:33 字数 342 浏览 6 评论 0原文

我有一个从 xml 文件返回的字符串,看起来与此类似,

name1="test 1" name2="test2" name3="test 3"

我想将其拆分为 key=value 的 3 个元素。我不能只使用空格进行拆分,因为我的某些值可能包含空格,例如 test 1。

因此,我想在空格之前使用 " 来拆分字符串。我尝试了很多变体,但不能找出正确的语法来指定我的分割字符是一个“后跟一个空格。例如,我尝试过 text.split({""" "}) 但它返回由 " 分割的元素并忽略 " 后面的空格。

不应该这么困难。有人可以帮我正确的语法吗?

I have an string that is returned from an xml file and looks similar to this

name1="test 1" name2="test2" name3="test 3"

I want to split this into 3 elements of key=value. I can't just split using a space because some of my values may contain spaces, e.g., test 1.

So, I'd like to split the string using the " before the space. I've tried many variations but can't figure out the correct syntax to specify that my split characters are a " followed by a space. For instance, I've tried text.split({""" "}) but that returns elements split by " and ignores the space after the ".

It shouldn't be this difficult. Can someone please help me with the correct syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后eg是否自 2025-01-02 11:17:33

试试这个:

text.Split(New String() {""" "}, StringSplitOptions.RemoveEmptyEntries)

更新

由于这将删除除最后一个元素之外的所有元素中的尾随“,因此在处理结果时需要考虑到这一点。

下面是一个示例:

    Dim sValue As String = "name1=""test 1"" name2=""test2"" name3=""test 3"""

    Dim asValues As String() = sValue.Split(New String() {""" "}, StringSplitOptions.RemoveEmptyEntries)

    For Each sKVP As String In asValues
        If Not sKVP.EndsWith("""") Then
            sKVP &= """"
        End If
        Console.WriteLine(sKVP)
    Next

以及结果输出:

name1="test 1"
name2="test2"
name3="test 3"

Try this:

text.Split(New String() {""" "}, StringSplitOptions.RemoveEmptyEntries)

Update

Since this will remove the trailing " in all but the last element, you need to account for this when processing the results.

Here is an example:

    Dim sValue As String = "name1=""test 1"" name2=""test2"" name3=""test 3"""

    Dim asValues As String() = sValue.Split(New String() {""" "}, StringSplitOptions.RemoveEmptyEntries)

    For Each sKVP As String In asValues
        If Not sKVP.EndsWith("""") Then
            sKVP &= """"
        End If
        Console.WriteLine(sKVP)
    Next

and the resultant output:

name1="test 1"
name2="test2"
name3="test 3"
神经暖 2025-01-02 11:17:33

您可以使用 LINQ 创建 Dictionary(Of String, String)

Dim q = From item In value.Split({""" "}, StringSplitOptions.RemoveEmptyEntries)
        Select item.Split("="c)

Dim dict = q.ToDictionary(Function(i) (i(0)),
                          Function(i) (i(1)))

注意:这不是自动防故障的,如果有重复的键,您'会得到一个例外。

You could use LINQ to create the Dictionary(Of String, String):

Dim q = From item In value.Split({""" "}, StringSplitOptions.RemoveEmptyEntries)
        Select item.Split("="c)

Dim dict = q.ToDictionary(Function(i) (i(0)),
                          Function(i) (i(1)))

Note: this is not fail-safe, f.e. if there are repeating keys, you'll get an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文