分割字符串并将分割段分配给变量
我从 txt 文件中读取了以下文本,但想将三个数字中的每一个分配给单独的变量,我怎样才能实现这一点?
(234.134, 105.087, 0.000000)
EDDIT:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Dim XVAL As String
Dim YVAL As String
Dim ZVAL As String
Dim strArr() As String
Dim count As Integer
str = "(0.123, 4.467, 8.910)"
strArr = str.Split(", ")
For count = 0 To strArr.Length - 3
XVAL = (strArr(count))
Next
For count = 0 To strArr.Length - 2
YVAL = (strArr(count))
Next
For count = 0 To strArr.Length - 1
ZVAL = (strArr(count))
Next
Label1.Text = XVAL + ZVAL
Label2.Text = YVAL
Label3.Text = ZVAL
End Sub
现在 XVAL 和 ZVAL 的乘积显示为 0.123 8.910,而不是 0.123 + 8.910 = 9.033
,即我之后的 9.033
I hjave the following text read from a txt file but would like to assingn each of the three numbers to induvidual variables how can i achive this?
(234.134, 105.087, 0.000000)
EDDIT:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Dim XVAL As String
Dim YVAL As String
Dim ZVAL As String
Dim strArr() As String
Dim count As Integer
str = "(0.123, 4.467, 8.910)"
strArr = str.Split(", ")
For count = 0 To strArr.Length - 3
XVAL = (strArr(count))
Next
For count = 0 To strArr.Length - 2
YVAL = (strArr(count))
Next
For count = 0 To strArr.Length - 1
ZVAL = (strArr(count))
Next
Label1.Text = XVAL + ZVAL
Label2.Text = YVAL
Label3.Text = ZVAL
End Sub
only now the product of XVAL and ZVAL is displayed as 0.123 8.910 and not 0.123 + 8.910 = 9.033
the 9.033 which is what im after
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我喜欢
string.Split
和TryParse
:由于这会提供一些重复的代码,因此可以将重复行为封装到一个方法中(稍后可能会用该方法更新答案;需要赶紧赶去火车......)。
I like
string.Split
andTryParse
:Since this gives a bit repetetive code, the repeating behavior can be encapsulated into a method (might update the answer with that later; need to rush off to the train...).
如果您的格式一致,则正则表达式将是获取此信息的好方法。如果将正则表达式的各个部分括在方括号中,则稍后可以使用 RegEx 结果上的 .Groups 属性检索字符串的这些部分的值。这是一个简单的示例:
您需要验证正则表达式是否正确(有很多在线网站,您可以在其中使用正则表达式,并且匹配组是否正确排列(您可以使用组来找出你需要什么。
Provided that your format is consistent, Regular Expressions would be a good approach to take with getting this information. If you enclose sections of a regular expression in brackets, you can later retrieve the value of those sections of the string using the .Groups property on your RegEx result. Here's a simple example:
You'll want to verify that the regular expression is correct (there's lots of sites online where you can play with regular expressions, and also that the matching groups line up properly (you can play with the Groups to figure out what you need.