【发布时间】:2016-04-17 21:16:32
【问题描述】:
我正在编写一个控制 3D 打印机的小程序。当我给它发送一些东西时,它通常会回复ok,但如果有什么不好的,它会发送如下内容:
T:221.0 /220.0 @:0 W:1
如果它有适当的分隔符,我可以很容易地解析它,但由于字符串221.0 /220.0,使用空格并不可靠。因此,如果我使用空格作为分隔符,/220.0 可能会被视为键值对,但没有键,因为它在 T 之下。我计划获取每个冒号的索引和它后面的简单开始 1 字符,但密钥长度也是可变的。例如:
T:221.0 /220.0 B@:127 @:0 W:1
B@ 现在是两个字符长。
我做了一些研究,但我发现的所有内容都有适当的分隔符,例如 URL with data。
http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.cld&datum=201311161500&cultuur=en-GB&continent=europa
我计划获取每个冒号的索引,然后在找到一个冒号时向后搜索一个空格作为起点。同样,下一个键值对的开始点将作为前一个键值对的结束点。但是,我不确定这是否是正确的方法。
主要问题: 如何解析没有适当分隔符的字符串? 我真的没有具体要求。无论是数组还是列表,为键和值单独变量,或者只是将所有内容都塞入一个数组中
string[] data = {key1,value1,key2,value2,key3,value3};
更新:以下是第二个示例中的键值对:
Key:Value
T:221.0 /220.0
B@:127
@:0
W:1
更多示例:
T:221.0 /220.0 B:85.7 /120 B@:30W @:0 W:8
Key:Value
T:221.0 /220.0
B:85.7 /120
B@:30W
@:0
W:8
这是另一个更复杂的:
T:171.4 /220.0 B:90.3 /120 T1:171.4 /220.0 B@:30 @:12W W:6
Key:Value
T:171.4 /220.0 // Temperature of first nozzle heater
B:90.3 /120 // Temperature of the hot plate it's printing on
T1:171.4 /220.0 // Temperature of the second nozzle heater if it exists
B@:30 // Raw power consumption of hotbed (unit depends on config)
@:12W // Power of of nozzle in Watts (unit depends on config)
W:6 // Waiting time (in seconds). If the optimal conditions are met and this counts down to zero, printing resumes. Else, reset to 10.
示例字符串开头的空格是有意的。它确实以空格开头。对于那些感兴趣的人,这些是运行 Marlin 3D 打印固件的 Arduino Mega 的回复。这些是打印机加热器还不够热而无法挤出时的回复。
【问题讨论】:
-
这里的键/值是什么: T:221.0 /220.0 B@:127 @:0 W:1 ?我打算在这里建议使用正则表达式。请添加几个输入样本,以便我们更清楚地了解您的输入内容。
标签: c# string parsing delimiter key-value