简体   繁体   English

如何在Visual Basic中将String转换为Integer以进行计算

[英]How to convert String to Integer for calculations in Visual Basic

I currently receive the input of the String from various input boxes in visual Basic. 我目前从Visual Basic的各种输入框中收到String的输入。 This is the added to a string to make a bigger string. 这是添加到字符串以组成更大的字符串。 The string looks like this (don't worry about the variable names): 字符串如下所示(不用担心变量名):

Dim String1 As String = "12345"

What I do after this is create a string with spaces between every digit. 之后,我要做的是创建一个字符串,每个数字之间都有空格。 This means the String gets converted to this (using a for loop): 这意味着将字符串转换为此(使用for循环):

Dim newString As String = "1 2 3 4 5"

For this I split the string into an Array using the split function whenever it comes across a String. 为此,只要遇到字符串,我都会使用split函数将字符串拆分为Array。 This create a new array called result() (As a String). 这将创建一个名为result()的新数组(作为字符串)。

When trying to read the individual items in the array, it works, and I get the correct results returned. 当尝试读取数组中的单个项目时,它可以工作,并且我得到了正确的返回结果。 Here is where the fun starts. 这就是乐趣的开始。 when accessing the items individually and converting them to integers using the "Convert.ToInt32" function works like a charm. 当单独访问这些项目并使用“ Convert.ToInt32”功能将其转换为整数时,其作用就像一个超级按钮。 It allows me to do what I want to do. 它允许我做我想做的事。 But, accessing every item in the array like this is not an option, as it will get out of control very quickly. 但是,像这样访问数组中的每个项目都不是一种选择,因为它将很快失去控制。 so what I'm trying to do is create a new for loop to go through the array, and convert every item to a integer as well as adding them together while converting them. 所以我想做的是创建一个新的for循环以遍历数组,并将每个项目转换为整数,以及在转换它们时将它们加在一起。 This is where the issue comes in. 这就是问题所在。

        Dim calcu As Integer = 0
    For i As Integer = 0 To result.Length - 1
        calcu += Convert.ToInt32(result(i))
    Next i

the code above is the code I currently use to add the new value to the array. 上面的代码是我当前用于将新值添加到数组的代码。 This however crashes and gives me the following error: "Input string was not in the correct format". 但是,这崩溃了,并给了我以下错误:“输入字符串的格式不正确”。 what method can I use to convert this string to an integer? 我可以使用什么方法将此字符串转换为整数?

what I'm trying to achieve is adding every value in the string together to give me a value in and integer. 我要实现的目标是将字符串中的每个值加在一起,以给我in和integer中的值。

You can use this approach to get your desired string where every digit is separated by space: 您可以使用这种方法来获取所需的字符串,其中每个数字都由空格分隔:

Dim chars As Char() = String1.ToCharArray() 
Dim newString As String = String.Join(" ", chars)

If you want to get the numeric value of each digit-char you can use Char.GetNumericValue : 如果要获取每个数字字符的数值,可以使用Char.GetNumericValue

Dim allInts As Int32() = Array.ConvertAll(chars, Function(c) CInt(Char.GetNumericValue(c)))

If you want the sum you can use Enumerable.Sum : 如果需要总和,可以使用Enumerable.Sum

Dim sumOfInts As Int32 = allInts.Sum()

Another option, which would allow the conversion to ignore everything that isn't a digit: 另一种选择是允许转换忽略数字以​​外的所有内容:

    Dim String1 As String = "dfdf##%^%&^%^%9 1  xfdsafadf   12dfdfd3  45"

    Dim ints As List(Of Integer) = String1.ToCharArray.Where(Function(x) Char.IsDigit(x)).Select(Function(x) CInt(Char.GetNumericValue(x))).ToList

    For Each i As Integer In ints
        Debug.Print(i)
    Next

Using different syntax: 使用不同的语法:

    Dim String1 As String = "dfdf##%^%&^%^%9 1  xfdsafadf   12dfdfd3  45"

    Dim ints = From i In String1.ToCharArray
               Where Char.IsDigit(i)
               Select CInt(Char.GetNumericValue(i))

    For Each i As Integer In ints
        Debug.Print(i)
    Next

Then, with either syntax, you can use Sum() as Tim demonstrated earlier: 然后,无论使用哪种语法,都可以使用Tim前面演示的Sum():

    Debug.Print("Sum = " & ints.Sum)

*Side-note: The ToCharArray() part in either version above isn't actually necessary because String implements IEnumerable(Of Char). *旁注:由于String实现IEnumerable(Of Char),因此上述两个版本中的ToCharArray()部分实际上都是不需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM