0

I am importing CSV file with multiple lines where the end of the line is a vbCrLf.

The problem is that sometimes I am getting into the fields with the value vbLf.

I would just like to replace the vbLf.

When i use the code below replaces the vbLf and vbCrLf as indicating the end of the line:

txt = txt.Replace ( vbLf , "")

How do I replace only the vbLf?

  • Use a regular expression with a [negative lookbehind](https://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspx) to assert there is no `vbCr` before the `vbLf`. – GSerg Feb 02 '16 at 17:03
  • @GSerg, how i create the negative in this case? Thanks – socialclub Feb 02 '16 at 17:16
  • Click the link, scroll down to "negative lookbehind", copy the construct to your regular expression and note that `vbCr` is `\r` and `vbLf` is `\n`. – GSerg Feb 02 '16 at 17:24
  • 1
    You are focusing on the wrong problem, you cannot use StreamReader to read such a file. It will detect the line feed character as a line ending. Writing your own custom StreamReader class, well, have fun. – Hans Passant Feb 02 '16 at 17:53
  • 1
    `txt = txt.Replace(vbLf, "")` should replace only `vbLf` - I don't see how it would replaace `vbCrLf`..... – Tim Feb 02 '16 at 19:01
  • 1
    @Tim, vbLf = \n, vbCr = \r, vbCrLf = \r\n, the replace remove "half" vbCrLf and modify it in vbCr – bdn02 Feb 02 '16 at 19:25
  • @HansPassant is right...OP check this: http://stackoverflow.com/questions/27223228/differences-between-vblf-vbcrlf-vbcr-constants – Hackerman Feb 02 '16 at 20:06

1 Answers1

0

For anyone in the future: If the line ends or starts with vbLf(Chr(10)) you can just TRIM each line, like so:

str = str.TrimStart(Chr(10))
str = str.Trim(Chr(10))
str = str.TrimEnd(Chr(10))
Moondoggie
  • 61
  • 1
  • 2
  • 9