473,511 Members | 14,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting NewLine characters in a string.

19 New Member
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
  4.  
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))
  3.  
  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()
  11.  
  12.         Dim del_1 = delimiters.Key, del_2 As String = delimiters.Value
  13.  
  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)
  17.  
  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
  25.  
  26.                 If curr_char = del_2 Then
  27.                     Exit For
  28.                 End If
  29.             End If
  30.         Next
  31.  
  32.         Return result
  33.     End Function
Thanks for any help, Alex.
Dec 2 '09 #1
5 3092
MrMancunian
569 Recognized Expert Contributor
Did you check the length of (filecontents.Length - 1)? I'm guessing from your explaination it's 1...

Steven
Dec 3 '09 #2
alex21
19 New Member
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
MrMancunian
569 Recognized Expert Contributor
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
Dec 4 '09 #4
alex21
19 New Member
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
Dec 4 '09 #5
MrMancunian
569 Recognized Expert Contributor
So, why don't you add an Exit For to the part where the NewLine is intercepted?

Steven
Dec 7 '09 #6

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

Similar topics

3
1963
by: mudge | last post by:
I'm having a problem getting rid of newlines. say I have this situation: $cat = "A nice\n cat"; The "\n" is an escape character which creates a newline right there. Now, how would I get...
5
57144
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
0
491
by: Howard Kaikow | last post by:
I have never seen WriteLine fail to produce a NewLine, at least, not until now: I was playing with an example given on pages 51-52 of the book "A Visual Basic 6 Programmer's Toolkit". The book...
0
791
by: Howard Kaikow | last post by:
I have never seen WriteLine fail to produce a NewLine, at least, not until now: I was playing with an example given on pages 51-52 of the book "A Visual Basic 6 Programmer's Toolkit". The book...
8
11394
by: Jm | last post by:
Hi all Is there a way to search a string and count the amount of newline or crlf's that are inside it ? Under vb6 i could search for vbcrlf using Instr and a loop. Is there a similar method...
4
4461
by: Peter Kirk | last post by:
Hi I would like to ask a little bit about the value Environment.Newline: what is it and what is the point of it? Ok, I can see in the docs that it represents "newline" for the current platform -...
5
6970
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the...
2
5656
by: John Salerno | last post by:
If I read a string that contains a newline character(s) into a variable, then write that variable to a file, how can I retain those newline characters so that the string remains on one line rather...
1
8486
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.