Author Topic: remove line feeds from a string  (Read 1583 times)

0 Members and 1 Guest are viewing this topic.

poncelet

  • Guest
remove line feeds from a string
« on: October 01, 2011, 09:29:42 AM »
Hello,
Is there a way to remove line feeds (empty lines) with regex?

Code: [Select]
output = Regex.Replace(output, "\r{2}", Environment.NewLine)
Code above doens't work  :-(

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: remove line feeds from a string
« Reply #1 on: October 01, 2011, 10:26:07 AM »
Hi,

This may work :
Code: [Select]
output = Regex.Replace(output , "\n{2}", "\n");Or this (depends on the output string):
Code: [Select]
output = Regex.Replace(output , "(\r\n){2}", "\n");
But it would be easier to reply if give some more precision, i.e. an example of 'output' string, or how do you get it...
« Last Edit: October 01, 2011, 10:45:22 AM by gile »
Speaking English as a French Frog

poncelet

  • Guest
Re: remove line feeds from a string
« Reply #2 on: October 01, 2011, 10:35:26 AM »
I tried it but it doesn't remove line feeds.

I import an ascii file with coordinates:
Quote
1   475830.758   4199125.634
2   475834.777   4199116.911
3   475840.666   4199101.86


4   475836.718   4199110.199

i can remove them with this code:
Quote
        Dim length As Long
        While length <> text.Length
            length = text.Length
            text = text.Replace(Environment.NewLine & Environment.NewLine, Environment.NewLine)
        End While
        If Microsoft.VisualBasic.Right(text, 1) = Chr(10) Then
            text = text.Remove(text.Length - 1)
        End If
        Return text
but i would like to use regex

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: remove line feeds from a string
« Reply #3 on: October 01, 2011, 10:51:45 AM »
Look at the second expression in my  first post, it may work with a string imported from an ascii file where a new line consist in a carriage return char plus a a new line char ("\r\n").
Speaking English as a French Frog

poncelet

  • Guest
Re: remove line feeds from a string
« Reply #4 on: October 01, 2011, 10:55:19 AM »
I'm going to try this too

Solution:
Code: [Select]
output = Regex.Replace(output, "[\r\n]+", Environment.NewLine) :lol:
I was trying for hours to find solution
Thank you gile!

Suddenly i got more than one options (haha!)