提问人:Fletchable 提问时间:6/21/2023 更新时间:6/21/2023 访问量:36
在 VB.NET 中区分字符串中的带引号和不带引号的短语
Distinguishing between quoted and non-quoted phrases within a string in VB.NET
问:
假设我有一个来自用户输入的字符串,其中包含带引号和不带引号的元素:
cmdArgs = "write "Hello, world" 10"
有没有一种可行的方法可以将其分解为三个单独的字符串 , , 和 ?任何帮助将不胜感激!Split()
write
Hello, world
10
答:
0赞
Fletchable
6/21/2023
#1
好的,我找到了一个合适的解决方法,即创建一个单独的数组,该数组使用单独的分隔符进行测量(谢谢,Andrew Mortimer!如果原始数组超过一定长度,它将遵从到新数组。cmdArgs
0赞
jmcilhinney
6/21/2023
#2
您可以使用 读取包含引号值的分隔文本:TextFieldParser
Dim str = "write ""Hello, world"" 10"
Using reader As New StringReader(str),
parser As New TextFieldParser(reader) With {.Delimiters = {" "}}
Dim fields = parser.ReadFields()
For Each field In fields
Console.WriteLine(field)
Next
End Using
输出:
write Hello, world 10
评论
Dim vals() As String = cmdArgs.Split(Convert.ToChar(""""))
TextFieldParser
cmdArgs = "write ""Hello, world"" 10"