473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 30816
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
9
by: ted | last post by:
I'm having trouble using the re module to remove empty lines in a file. Here's what I thought would work, but it doesn't: import re f = open("old_site/index.html") for line in f: line =...
2
by: rorley | last post by:
I'm trying to open a text file, remove all instances of the words "f=x;" and "i=x;" where x can be any number 0-14. Also, I want to remove all { " or ) or ( or ' } each time one of those...
11
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
6
by: Lisa | last post by:
I am reading in data from a text file. I want to enter each value on the line into a list and retain the order of the elements. The number of elements and spacing between them varies, but a...
11
by: cody | last post by:
Is there a method to replace special characters like Ä (A-Umlaut) with A, Ö (O-Umlaut) with O, and so on? Sure, I could look for each character separately and replace it with its...
11
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET that leaves empty tags in the markup. I need a...
36
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have an array of strings. What is the simplest way to remove the elements that are empty, i.e. where the expression "empty($elt)" returns true? Thanks, - Dave
6
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.