473,548 Members | 2,716 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 3095
MrMancunian
569 Recognized Expert Contributor
Did you check the length of (filecontents.L ength - 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.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?
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
1968
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 rid of the newline in that string?
5
57155
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 could get only 1st line, but it is not returning 2nd line. here's code: string address = null; string ssep = Environment.NewLine; char sep =...
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 is accompanied by files for that chapter in which an example uses VB 6's Print to output to a Form.. When upgrading the code to VB .NET 2002, the...
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 is accompanied by files for that chapter in which an example uses VB 6's Print to output to a Form.. When upgrading the code to VB .NET 2002, the...
8
11397
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 under .net ?
4
4462
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 - I assume that it is a runtime property, and not compile time? But won't it always be the same anyway - does dotnet run on anything other than...
5
6980
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 mail. Thanks...
2
5660
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 than spans multiple lines? Example: In a CGI script, I'm reading the address field from an HTML form. Almost always the person will press ENTER...
1
8491
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
7444
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7954
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6039
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.