By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,456 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,456 IT Pros & Developers. It's quick & easy.

Detecting NewLine characters in a string.

P: 19
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

Expand|Select|Wrap|Line Numbers
  1. if del_2 = ControlChars.NewLine then
  2.   MessageBox.Show("It is a newline")
  3. 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.

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function DetermineColumns(ByVal filepath As String, ByVal delimiters As KeyValuePair(Of String, String)) As List(Of KeyValuePair(Of String, Type))
  2.         Dim result As New List(Of KeyValuePair(Of String, Type))
  4.         Dim reader As New StreamReader(filepath)
  5.         Dim filecontents As String = ""
  6.         While reader.EndOfStream() = False
  7.             filecontents = filecontents & reader.ReadLine() & ControlChars.NewLine
  8.         End While
  9.         reader.Close()
  10.         reader.Dispose()
  12.         Dim del_1 = delimiters.Key, del_2 As String = delimiters.Value
  14.         Dim read As String = ""
  15.         For i As Integer = 0 To (filecontents.Length - 1) Step 1
  16.             Dim curr_char As String = filecontents(i)
  18.             If curr_char <> del_1 And curr_char <> del_2 Then
  19.                 read = read & curr_char
  20.             Else
  21.                 If curr_char = del_1 Then
  22.                     result.Add(New KeyValuePair(Of String, Type)(String.Format("{0}_{1}", DetermineDataType(read).Name, i.ToString()), DetermineDataType(read)))
  23.                     read = ""
  24.                 End If
  26.                 If curr_char = del_2 Then
  27.                     Exit For
  28.                 End If
  29.             End If
  30.         Next
  32.         Return result
  33.     End Function
Thanks for any help, Alex.
Dec 2 '09 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 569
Did you check the length of (filecontents.Length - 1)? I'm guessing from your explaination it's 1...

Dec 3 '09 #2

P: 19
Nah there are many newline characters before the end of the stream, and filecontents.Length - 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.Split(ControlChars.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?
Dec 3 '09 #3

Expert 100+
P: 569
Check your code between line 26 and 28. This is how I read it: Whenever it enters that If-clause, you exit the, which means that your function will end...Is that what you want?

Dec 4 '09 #4

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

Dec 4 '09 #5

Expert 100+
P: 569
So, why don't you add an Exit For to the part where the NewLine is intercepted?

Dec 7 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.