分割字符串并将分割段分配给变量

发布于 2024-10-04 13:51:50 字数 888 浏览 5 评论 0原文

我从 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 技术交流群。

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

发布评论

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

评论(2

国粹 2024-10-11 13:51:50

我喜欢 string.SplitTryParse

string input = "(234.134, 105.087, 0.000000)";
string[] numbers = input.Split(
                       new[] { ',', ')', '(' }, 
                       StringSplitOptions.RemoveEmptyEntries);
double x, y, z;
if (double.TryParse(numbers[0], 
                    NumberStyles.Any, 
                    CultureInfo.InvariantCulture, 
                    out x))
{
    // first string was not a valid number
}

if (double.TryParse(numbers[1], 
                    NumberStyles.Any, 
                    CultureInfo.InvariantCulture, 
                    out y))
{
    // second string was not a valid number
}

// and so on

由于这会提供一些重复的代码,因此可以将重复行为封装到一个方法中(稍后可能会用该方法更新答案;需要赶紧赶去火车......)。

I like string.Split and TryParse:

string input = "(234.134, 105.087, 0.000000)";
string[] numbers = input.Split(
                       new[] { ',', ')', '(' }, 
                       StringSplitOptions.RemoveEmptyEntries);
double x, y, z;
if (double.TryParse(numbers[0], 
                    NumberStyles.Any, 
                    CultureInfo.InvariantCulture, 
                    out x))
{
    // first string was not a valid number
}

if (double.TryParse(numbers[1], 
                    NumberStyles.Any, 
                    CultureInfo.InvariantCulture, 
                    out y))
{
    // second string was not a valid number
}

// and so on

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...).

格子衫的從容 2024-10-11 13:51:50

如果您的格式一致,则正则表达式将是获取此信息的好方法。如果将正则表达式的各个部分括在方括号中,则稍后可以使用 RegEx 结果上的 .Groups 属性检索字符串的这些部分的值。这是一个简单的示例:

Dim toMatch as String = "(234.134, 105.087, 0.00000)"
Dim regEx as Regex = new Regex("\((\d*(\.\d+)?), (\d*(\.\d+)?), (\d*(\.\d+)?)\)")

Dim match as Match = regEx.Match(toMatch)
Dim var1 as Float = Float.Parse(match.Groups(1).Value)
Dim var2 as Float = Float.Parse(match.Groups(3).Value)
Dim var3 as Float = Float.Parse(match.Groups(5).Value)

您需要验证正则表达式是否正确(有很多在线网站,您可以在其中使用正则表达式,并且匹配组是否正确排列(您可以使用组来找出你需要什么。

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:

Dim toMatch as String = "(234.134, 105.087, 0.00000)"
Dim regEx as Regex = new Regex("\((\d*(\.\d+)?), (\d*(\.\d+)?), (\d*(\.\d+)?)\)")

Dim match as Match = regEx.Match(toMatch)
Dim var1 as Float = Float.Parse(match.Groups(1).Value)
Dim var2 as Float = Float.Parse(match.Groups(3).Value)
Dim var3 as Float = Float.Parse(match.Groups(5).Value)

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.

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