VB.net - 如何使用“分割字符串”后跟一个空格作为分隔符
我有一个从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
更新
由于这将删除除最后一个元素之外的所有元素中的尾随“,因此在处理结果时需要考虑到这一点。
下面是一个示例:
以及结果输出:
Try this:
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:
and the resultant output:
您可以使用 LINQ 创建
Dictionary(Of String, String)
:注意:这不是自动防故障的,如果有重复的键,您'会得到一个例外。
You could use LINQ to create the
Dictionary(Of String, String)
:Note: this is not fail-safe, f.e. if there are repeating keys, you'll get an exception.