?
Solved

How to delete CR LF in a .csv file using Vb.Net

Posted on 2014-09-23
6
Medium Priority
?
859 Views
Last Modified: 2014-09-23
I am working on VS 2012, Vb.Net - .Net framework 4.0

This piece of code, read the .csv file and expected to rewrite the .csv file without Carriage Return.

But now, Carriage Return is created as a new line. I have also attached the screen shot.

Along with the post, i have attached the sample csv file.

 Public Sub Test()
        Try

            Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead("D:\CSV\Test.csv"))
            Dim listA As New List(Of String)()


            If File.Exists("d:\CSV\TestOut1.csv") Then
                File.Delete("d:\CSV\TestOut1.csv")
            End If

            Dim sw As New StreamWriter("d:\CSV\TestOut1.csv")
            Dim s As String = String.Empty

            While reader.Peek() >= 0
                Dim line As String = reader.ReadLine()
                Dim values As String() = line.Split(";"c)
                listA.Add(values(0))
                s = s + line + Chr(10)
            End While
            reader.Close()
            sw.WriteLine(s)
            sw.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

Open in new window

D--CSV-Expected-Output.jpg
D--CSV-Test.csv
0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:skipper68
ID: 40339633
have you tried .replace(vbcrlf, "") on the 'line' or the split array of items?
Dim values As String() = line.Split(";"c).replace(vbCrLf,"")
or
listA.Add(values(0).replace(vbCrLf,""))
0
 
LVL 14

Expert Comment

by:Don Thomson
ID: 40339639
search for the ascii character combination of CRLF  (13 and 10) and and don't write a new line

Or simply search for a  line with a length of less than 3 characters and ignore it

Not sure why your doing this - a .csv  file will always be created with the CRLF  at the end.
Every program that I've seen that imports a CSV file uses the CRLF as an end of file and stops writing
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40339670
Change this line of code

sw.WriteLine(s)

to this

sw.Write(s)

And see if that is what you are looking for.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chokka
ID: 40339840
@Fernando, Great !! Thank you !! Looks like it works !!

I need to test again to confirm
0
 

Author Closing Comment

by:chokka
ID: 40340482
Thank you !!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40340542
Not a problem, glad I was able to help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question