Results 1 to 4 of 4

Thread: [2005] Search a string for ControlChars.CrLf

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    [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:
    1. InStr(txtFileContents.Text, ControlChars.CrLf)

    Thanks
    Shannon

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim firstLineBreak As Integer = txtFileContents.Text.IndexOf(ControlChars.CrLf)
    2. Dim firstLineFeed As Integer = txtFileContents.Text.IndexOf(ControlChars.Lf)
    3.  
    4. If firstLineBreak = -1 Then
    5.     MessageBox.Show("No line Windows line breaks")
    6. Else
    7.     MessageBox.Show("First Windows line break at " & firstLineBreak)
    8. End If
    9.  
    10. If firstLineFeed = -1 Then
    11.     MessageBox.Show("No line line feeds")
    12. Else
    13.     MessageBox.Show("First line feed at " & firstLineFeed)
    14. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    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

  4. #4
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [2005] Search a string for ControlChars.CrLf

    ReadLine() only reads the current line of the stream while ReadToEnd() reads the whole stream.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width