简体   繁体   中英

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

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. This create a new array called result() (As a String).

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

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 :

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

If you want the sum you can use 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:

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

*Side-note: The ToCharArray() part in either version above isn't actually necessary because String implements IEnumerable(Of Char).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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