By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Fastest way to strip NewLine from string (.Net 2, VS 2005)

P: n/a
Hello group,

What is the fastest way to remove line feeds from the end of a string?
RegEx was too slow. I am using a for loop (code below), but that seems
too cumbersome.

Any suggestions appreciated.

J Wolfgang Goerlich
string logText = txtOutput.Text;
string s10 = Convert.ToChar(10).ToString();
string s13 = Convert.ToChar(13).ToString();

for (int i = logText.Length - 1; i 0; i--)
{
string s = logText.Substring(i, 1);
if (s == s10 | s == s13)
{
logText = logText.Remove(i);
i--;
}
else
{
break;
}
}

txtOutput.Text = logText;

Feb 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 25 Feb 2007 09:44:27 -0800, jw********@gmail.com wrote:
>Hello group,

What is the fastest way to remove line feeds from the end of a string?
RegEx was too slow. I am using a for loop (code below), but that seems
too cumbersome.

Any suggestions appreciated.

J Wolfgang Goerlich
String.TrimEnd() ?

--
Philip Daniels
Feb 25 '07 #2

P: n/a
Ha! Yes, much better. Whew.

logText = logText.TrimEnd(Environment.NewLine.ToCharArray()) ;

Thank you.

J Wolfgang Goerlich

On Feb 25, 12:57 pm, PhilipDani...@foo.com wrote:
On 25 Feb 2007 09:44:27 -0800, jwgoerl...@gmail.com wrote:
Hello group,
What is the fastest way to remove line feeds from the end of a string?
RegEx was too slow. I am using a for loop (code below), but that seems
too cumbersome.
Any suggestions appreciated.
J Wolfgang Goerlich

String.TrimEnd() ?

--
Philip Daniels

Feb 25 '07 #3

P: n/a
jw********@gmail.com wrote:
Ha! Yes, much better. Whew.

logText = logText.TrimEnd(Environment.NewLine.ToCharArray()) ;

Thank you.

J Wolfgang Goerlich

On Feb 25, 12:57 pm, PhilipDani...@foo.com wrote:
>On 25 Feb 2007 09:44:27 -0800, jwgoerl...@gmail.com wrote:
>>Hello group,
What is the fastest way to remove line feeds from the end of a string?
RegEx was too slow. I am using a for loop (code below), but that seems
too cumbersome.
Any suggestions appreciated.
J Wolfgang Goerlich
String.TrimEnd() ?

--
Philip Daniels

Well, your code can be made a bit less cumbersome also.

Here's a simplified version of your code:

for (int i = logText.Length - 1; i 0; i--) {
char s = logText[i];
if (s == '\n' | s == '\r') {
logText = logText.Remove(i);
} else {
break;
}
}

Now, let's optimise it by only doing the remove once:

int i = logText.Length;
while (i 0 && ((char s = logText[i-1]) == '\r' || s == '\n') i--;
if (i < logText.Length) logText = logText.Remove(i);

Just a small detour into the land of optimising.
:)

--
Göran Andersson
_____
http://www.guffa.com
Feb 25 '07 #4

P: n/a
Göran Andersson wrote:
Now, let's optimise it by only doing the remove once:

int i = logText.Length;
while (i 0 && ((char s = logText[i-1]) == '\r' || s == '\n') i--;
if (i < logText.Length) logText = logText.Remove(i);

Just a small detour into the land of optimising.
What about a detour into a compiler ?

Arne

Feb 26 '07 #5

P: n/a
Arne Vajhřj wrote:
Göran Andersson wrote:
>Now, let's optimise it by only doing the remove once:

int i = logText.Length;
while (i 0 && ((char s = logText[i-1]) == '\r' || s == '\n') i--;
if (i < logText.Length) logText = logText.Remove(i);

Just a small detour into the land of optimising.

What about a detour into a compiler ?

Arne
Interresting question. The principle of the code works, there is just
two small mistakes in it.

I think that you should be able to spot them yourself. :)

--
Göran Andersson
_____
http://www.guffa.com
Feb 27 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.