473,799 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\n2 3\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 31049
On Apr 5, 4:14 pm, LEM <anonym...@nosp am.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\n2 3\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.New Line 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\n2 3\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(p p.Length +
Environment.New Line.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.AppendLi ne(line);
}
}

Now you have the result in the StringBuilder.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

using (
"LEM" <an*******@nosp am.comwrote in message
news:OS******** ******@TK2MSFTN GP04.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\n2 3\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.New Line 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\n2 3\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...@nosp am.comwrote:
The Trim method should work.. also, you may want to use
Environment.New Line 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.Collecti ons.Generic;
using System.Text;

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

string foo = "\r\n\r\n1\r\n2 3\r\n\r\n4";
foo = RemoveEmptyLine s(foo);
}

public static string RemoveEmptyLine s(string hasEmptyLines)
{
string emptyLinesRemov ed = string.Empty;

string[] lines = hasEmptyLines.S plit(new string[]
{ Environment.New Line }, StringSplitOpti ons.RemoveEmpty Entries);

foreach (string line in lines)
{
emptyLinesRemov ed += line + Environment.New Line;
}

return emptyLinesRemov ed;
}
}
}

----

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
8903
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 a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
9
23520
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 = re.sub(r'^\s+$|\n', '', line) print line
2
2117
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 characters occurs respectively. This is what I've been able to piece together... import os, string x = ("f=;") y = ("i=;) inputFile = open('abcd.txt','r')
11
18268
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 size of the collection" Not sure what is going on... any suggestions welcome! public static string fixStr(string aStr) { StringBuilder bStr = new StringBuilder(aStr.Length); for (int i = 0; i < aStr.Length; i++)
6
2936
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 typical line looks like: ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 ' Why does the following not work: line = ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 '
11
14893
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 ascii-counterpart, but there are also such special characters in French and Swedish and many other languages which I also want to catch. Is there a generic way to do it?
11
3322
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 javascript solution to go behind and do a clean up after the page loads. The .NET will leave behind any combination of nested tags. Here is an example below. Even though the <spantags are not empty, as they contain <emtags they also need to be...
36
9191
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
4257
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 this program has more then one class and also uses an interface. Right now I am working on ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is...
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.