Detecting NewLine characters in a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alex21
    New Member
    • Oct 2008
    • 19

    Detecting NewLine characters in a string.

    I am trying to write a function for determining the data type of columns in a delimited file.

    However my function is not detecting a newline and exiting the loop after the end of the first line.

    * 'del_2' is equal to a new line character, i have tested this using a small bit of code

    Code:
    if del_2 = ControlChars.NewLine then
      MessageBox.Show("It is a newline")
    end if
    and got the message, and i know the newline characters are at the end of each line in 'filecontents' as i am adding them manually.

    Code:
    Public Shared Function DetermineColumns(ByVal filepath As String, ByVal delimiters As KeyValuePair(Of String, String)) As List(Of KeyValuePair(Of String, Type))
            Dim result As New List(Of KeyValuePair(Of String, Type))
    
            Dim reader As New StreamReader(filepath)
            Dim filecontents As String = ""
            While reader.EndOfStream() = False
                filecontents = filecontents & reader.ReadLine() & ControlChars.NewLine
            End While
            reader.Close()
            reader.Dispose()
    
            Dim del_1 = delimiters.Key, del_2 As String = delimiters.Value
    
            Dim read As String = ""
            For i As Integer = 0 To (filecontents.Length - 1) Step 1
                Dim curr_char As String = filecontents(i)
    
                If curr_char <> del_1 And curr_char <> del_2 Then
                    read = read & curr_char
                Else
                    If curr_char = del_1 Then
                        result.Add(New KeyValuePair(Of String, Type)(String.Format("{0}_{1}", DetermineDataType(read).Name, i.ToString()), DetermineDataType(read)))
                        read = ""
                    End If
    
                    If curr_char = del_2 Then
                        Exit For
                    End If
                End If
            Next
    
            Return result
        End Function
    Thanks for any help, Alex.
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    Did you check the length of (filecontents.L ength - 1)? I'm guessing from your explaination it's 1...

    Steven

    Comment

    • alex21
      New Member
      • Oct 2008
      • 19

      #3
      Nah there are many newline characters before the end of the stream, and filecontents.Le ngth - 1 prevents an IndexOutofRange exception which i would get if i removed the - 1.

      And i know the newline characters are there as i can use them to manipulate the string, such as filecontents.Sp lit(ControlChar s.NewLine) will split the string in the correct places.

      My only explanation is the way i am reading characters one by one, im thinking new line characters are longer then one character?

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        Check your code between line 26 and 28. This is how I read it: Whenever it enters that If-clause, you exit the for...next-loop, which means that your function will end...Is that what you want?

        Steven

        Comment

        • alex21
          New Member
          • Oct 2008
          • 19

          #5
          yeah as soon as it hits the newline i want it to send, however the function doesn't end and this is the problem.

          Thanks

          Comment

          • MrMancunian
            Recognized Expert Contributor
            • Jul 2008
            • 569

            #6
            So, why don't you add an Exit For to the part where the NewLine is intercepted?

            Steven

            Comment

            Working...