首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查至整数

查至整数
EN

Stack Overflow用户
提问于 2015-08-21 07:51:47
回答 3查看 98关注 0票数 0

我想从一个字符串中得到数字,我就是这么做的。

我想将这个字符转换为数据库的任何一个接口,但是得到以下错误:'/‘应用程序中的服务器错误。

输入字符串的格式不正确。

代码语言:javascript
复制
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.

代码:

代码语言:javascript
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-21 08:00:35

由于字符串中有多个数字,因此需要分别解析每个数字。

代码语言:javascript
复制
For Each ch As Char In myChars
    If Integer.TryParse(ch, iChar) Then
        Exit For
    End If
Next
票数 2
EN

Stack Overflow用户

发布于 2015-08-21 08:10:26

不需要转换字符串,您可以以Char值的形式访问字符串中的字符。您可以使用Char.IsDigit检查数字:

代码语言:javascript
复制
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
票数 5
EN

Stack Overflow用户

发布于 2015-08-21 09:46:07

您可以使用LINQ获取第一个数字,并得到没有数字的指示:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32135082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档