Readind a texte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CharChabil
    New Member
    • Oct 2006
    • 17

    Readind a texte

    Hey guys
    i want to read from file that contains the following data:
    -------------------
    A1/EXT "BK82 LB73 21233" 105 061018 1804
    EXTERNAL ALARM
    COOLING ALARM
    A1
    ------------------
    the thing is that i want to read each part alone
    and stores it in a database
    any hint for that?
    PS: how to read the double quote ?
    Regards,
  • xtab
    New Member
    • Oct 2006
    • 6

    #2
    One of the best ways to get round the problem of referring to double quotes is to use its ASCII value instead. This is 34.

    So as an example of the kind of thing you want to do (but without saving to a database - just displaying what it finds within the quotes), you can use something like this:

    Code:
            Dim src As String = "C:\Parts.txt"  ' name of file
            Dim sr As New IO.StreamReader(src)
            Dim checkStr As String = String.Empty
            Dim result As New ArrayList
            Do While Not sr.Peek = -1
                checkStr = sr.ReadLine
                If checkStr.Contains(Chr(34)) Then
                    Dim first As Integer = checkStr.IndexOf(Chr(34)) + 1
                    Dim last As Integer = checkStr.LastIndexOf(Chr(34))
                    result.Add(checkStr.Substring(first, last - first))
                End If
            Loop
    
            '  Display for demo purposes
            For Each s As String In result
                Console.WriteLine(s)
            Next
    This code will store the values from between the quotes in any line that has two sets of double quotes in it.
    Of course, to make it robust you would need to build in Exception Handling for cases e.g. where there is only one double quote. But if the layout of the data is always as you posted, then the above code will work fine.

    Comment

    Working...