1

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.

4
  • newString.Split(' ').Sum(Function(s) Integer.Parse(s)) Commented Aug 10, 2015 at 13:38
  • How are you declaring and populating your object named 'result' ? Commented Aug 10, 2015 at 13:39
  • Currently I'm declaring and populating the string as follows: Dim result() As String = Split(String1, " ") Commented Aug 10, 2015 at 13:40
  • Why don't you have the TextBoxes in an array that you can access via loop or Linq to do your calculations? Commented Aug 10, 2015 at 13:50

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.