469,621 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

Remove empty line from string

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
Apr 5 '07 #1
5 29512
On Apr 5, 4:14 pm, LEM <anonym...@nospam.comwrote:
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.
Apr 5 '07 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

using (
"LEM" <an*******@nospam.comwrote in message
news:OS**************@TK2MSFTNGP04.phx.gbl...
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

Apr 5 '07 #3
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.
Apr 5 '07 #4
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");

Apr 6 '07 #5
On Apr 5, 8:43 pm, LEM <anonym...@nospam.comwrote:
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.

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

Apr 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by tornado | last post: by
9 posts views Thread by ted | last post: by
2 posts views Thread by rorley | last post: by
11 posts views Thread by David | last post: by
36 posts views Thread by laredotornado | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.