TheSwamp

Code Red => .NET => Topic started by: poncelet on October 01, 2011, 09:29:42 AM

Title: remove line feeds from a string
Post by: poncelet 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  :-(
Title: Re: remove line feeds from a string
Post by: gile 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...
Title: Re: remove line feeds from a string
Post by: poncelet 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
Title: Re: remove line feeds from a string
Post by: gile 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").
Title: Re: remove line feeds from a string
Post by: poncelet 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!)