[2005] Search a string for ControlChars.CrLf
I am passing in a string using a streamreader. I am trying to find if a line uses CrLf, Cr, or LF. I have tried using an Instr but it always returns 0. What am I doing wrong? Should I be using something else?
VB Code:
InStr(txtFileContents.Text, ControlChars.CrLf)
Thanks
Shannon
Re: [2005] Search a string for ControlChars.CrLf
CrLf is a Windows line break, which consists of a carriage return and a line feed. If your string doesn't contain that combination then it can't be found. If your string only contains line feeds then you need to search for line feeds only. Line feeds on their own are used as line breaks on most other OSes, so Windows will normally treat them as line breaks too. Try this:
VB Code:
Dim firstLineBreak As Integer = txtFileContents.Text.IndexOf(ControlChars.CrLf)
Dim firstLineFeed As Integer = txtFileContents.Text.IndexOf(ControlChars.Lf)
If firstLineBreak = -1 Then
MessageBox.Show("No line Windows line breaks")
Else
MessageBox.Show("First Windows line break at " & firstLineBreak)
End If
If firstLineFeed = -1 Then
MessageBox.Show("No line line feeds")
Else
MessageBox.Show("First line feed at " & firstLineFeed)
End If
Re: [2005] Search a string for ControlChars.CrLf
Thanks for the quick reply. You code worked great. My problem was I was using the sr.readline instread of sr.readtoend. We get fixed width files in from clients that are generated from a mainframe and they say they are CRLF and alot of times they use LF and our ETL tool needs to know what type so I trying to write an app that will check and put the correct line terminator in. Again, thanks for your help.
Shannon
Re: [2005] Search a string for ControlChars.CrLf
ReadLine() only reads the current line of the stream while ReadToEnd() reads the whole stream.