Remove empty line from string

L

LEM

Hi,

I'm trying to remove any empty lines from a string, and I am doing the
following:

String pp;
pp = "\r\n\r\n1\r\n23\r\n\r\n4";

pp = pp.Replace("\r\n\r\n", "\r\n");

That works fine if the first line of the string is NOT empty.
How could I remove the first line as well?


Thanks
 
A

Andy

I'm trying to remove any empty lines from a string, and I am doing the
following:

String pp;
pp = "\r\n\r\n1\r\n23\r\n\r\n4";

pp = pp.Replace("\r\n\r\n", "\r\n");

That works fine if the first line of the string is NOT empty.
How could I remove the first line as well?

The Trim method should work.. also, you may want to use
Environment.NewLine instead of \r\n.
 
N

Nicholas Paldino [.NET/C# MVP]

LEM,

I think that there is an easier way to do this:

string pp = "\r\n\r\n1\r\n23\r\n\r\n4";

// Cycle through the string and remove all empty lines.
string line = null;

// The result. You pass the length of the string for the capacity, because
you might not remove anything.
// Additionally, you add the length of the line terminator because if the
original string didn't have a
// new line character sequence at the end, you will be adding one at the end
of the following loop,
// so you need to know the length of those characters.
StringBuilder result = new StringBuilder(pp.Length +
Environment.NewLine.Length);

// Use a StringReader.
using (StringReader reader = new StringReader(pp))
{
// Cycle through while there are lines to read.
while ((line = reader.ReadLine()) != null)
{
// Add the line with a newline.
result.AppendLine(line);
}
}

Now you have the result in the StringBuilder.

Hope this helps.
 
L

LEM

The Trim method should work.. also, you may want to use
Environment.NewLine instead of \r\n.

Andy, that will not work, because it will delete the empty lines
and the end of lines. I just want to delete the empty lines.
 
E

ent555

How could I remove the first line as well?

Try this:

String pp;
pp = "\r\n\r\n1\r\n23\r\n\r\n4";
pp = pp.TrimStart();
pp = pp.Replace("\r\n\r\n", "\r\n");
 
T

thoward37

Andy, that will not work, because it will delete the empty lines
and the end of lines. I just want to delete the empty lines.


try this...

using System;
using System.Collections.Generic;
using System.Text;

namespace RemoveEmptyLinesExample
{
class Program
{
static void Main(string[] args)
{

string foo = "\r\n\r\n1\r\n23\r\n\r\n4";
foo = RemoveEmptyLines(foo);
}

public static string RemoveEmptyLines(string hasEmptyLines)
{
string emptyLinesRemoved = string.Empty;

string[] lines = hasEmptyLines.Split(new string[]
{ Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

foreach (string line in lines)
{
emptyLinesRemoved += line + Environment.NewLine;
}

return emptyLinesRemoved;
}
}
}



----

if that ends up being too slow (because your source string has a lot
of lines) switch it over to use a string builder instead of += on the
string concatenation..

Hope that helps,
Troy Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top