IndexOf方法VB.net

时间:2011-05-01 14:45:11

标签: vb.net

如何使用Indexof Method搜索索引号?该文件的每一行的数字都不同。每个数组都有一个名称和一个不同的邮政编码。我想告诉它搜索行中的第一个数字。该索引之前的所有内容都将是名字,姓氏,然后是邮政编码。

infile = IO.File.OpenText("Names.txt")

    'process the loop instruct until end of file
    intSubscript = 0
    Do Until infile.Peek = -1
        'read a line
        strLine(intSubscript) = infile.ReadLine
        intSubscript = intSubscript + 1


    Loop

    infile.Close()

1 个答案:

答案 0 :(得分:0)

根据我的理解解决方案:

您可以将文件的每个部分保存在另一行(ReadLine),而不是使用IndexOf。

如果你真的需要IndexOf:它只是String.IndexOf(EnterCharacterHere)

您也可以阅读此文件并仅使用找到的数字:

首先创建一个const字符串const cstrNumbers as string = "0123456789",然后执行以下操作:

For x as integer = 0 to strInput -1
    strTemporary = strInput.Substring(x,1)
    If InStr(cstrNumbers, strTemporary) <> 0 Then
         strOutput &= strTemporary

strOutput将包含数字。

希望这有帮助,

西蒙

编辑: 使用数据库会更容易,但我没有在vb.net中使用db的经验。

你可以使用我提到的InStr做一个子串。

首先,您需要一个能够返回第一个数字的函数。 (使用InStr)

然后在子串(String.SubString(FirstOccurence, LengthOfZip)

中使用它

现在没时间做完整的代码了。希望这对你有所帮助