0

I wanted to get the numerical numbers from a string, which I did.

I want to convert this char to any interger for the database but get the following error: Server Error in '/' Application.

Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: Input string was not in a correct format.

Code:

For Each File As FileInfo In New DirectoryInfo(("C:\Folder")).GetFiles("*.aspx", SearchOption.AllDirectories)
    vFileName = File.FullName
                    Dim myChars() As Char = vFileName.ToCharArray()
                    Dim iChar As Integer = Convert.ToInt32(myChars)
                    iChar = Mid(1, 1, iChar)
GvS
  • 52,015
  • 16
  • 101
  • 139
indofraiser
  • 1,014
  • 3
  • 18
  • 50
  • 1
    What is this supposed to do? Show example input and output. What part of the filename contains the numbers you want to parse? What do you expect `Convert.ToInt32(char[])` to do? – CodeCaster Aug 21 '15 at 07:54
  • I want to get the first numerical character from the string vFileName. (which is dervided from the filename found in the folder it is searching though) i.e. x123.aspx would return 1 as an integer – indofraiser Aug 21 '15 at 07:56
  • That's not what this code does. You may be able to solve your first problem (_"Get the first numeric character from a string"_) by using a RegEx. – CodeCaster Aug 21 '15 at 07:58

3 Answers3

5

You don't need to convert the string, you can access the characters in it as Char values. You can use the Char.IsDigit to check for digits:

vFileName = File.FullName
Dim iChar As Integer = -1
For Each ch As Char In vFileName
  If Char.IsDigit(ch) Then
    iChar = Int32.Parse(ch)
    Exit For
  End If
Next
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Since there are multiple numbers in the string, you are going to need to parse each one individually.

For Each ch As Char In myChars
    If Integer.TryParse(ch, iChar) Then
        Exit For
    End If
Next
Whitey
  • 431
  • 2
  • 6
  • Please read the comments. OP wants to parse the first number occurring in the filename string, not the entire string. – CodeCaster Aug 21 '15 at 08:02
  • 1
    Ok I added an Exit For to the code. This will give you the first number in the string. – Whitey Aug 21 '15 at 08:03
  • 1 returns 49 but right track, just reading http://stackoverflow.com/questions/3665978/c-sharp-why-does-this-return-49-convert-toint321 – indofraiser Aug 21 '15 at 08:05
  • Try the new code I just edited in. It's because you're trying to convert a Char to Int32. This new code should work properly though. You could also use the old code and use iChar = Convert.ToInt32(CStr(ch)) – Whitey Aug 21 '15 at 08:07
  • 1
    No problem. Good luck :) – Whitey Aug 21 '15 at 08:09
  • @indofraiser Note that if there are no digits in the filename then that code will leave `iChar` with a value of zero. – Andrew Morton Aug 21 '15 at 08:19
0

You can use LINQ to get the first digit and get an indication of there being no digit:

Option Infer On

Imports System.IO

Module Module1

    Sub Main()
        Dim srcDir = "D:\inetpub\wwwroot" ' sample data

        ' Use Directory.EnumerateFiles if you want to get them incrementally, e.g.
        ' showing the list of files using AJAX.

        For Each f In Directory.GetFiles(srcDir, "*.aspx", SearchOption.AllDirectories)

            Dim num = Path.GetFileNameWithoutExtension(f).FirstOrDefault(Function(c) Char.IsDigit(c))

            If num <> Chr(0) Then
                Console.WriteLine("First digit is " & num)
            Else
                Console.WriteLine("No digit.")
            End If

        Next

        Console.ReadLine()

    End Sub

End Module
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84