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

Replace "\r\n" by "_"

P: n/a
Hi,

I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');

and it doesn't work, what is missing?

Thank you!
Marty
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
WRH
Hello
try...

strMyString = strMyString.Replace("\r\n","_");

"Marty" <xm******@hotmail.com> wrote in message
news:PzK7f.55138$Io.27031@clgrps13...
Hi,

I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');
and it doesn't work, what is missing?

Thank you!
Marty

Nov 17 '05 #2

P: n/a
VJ
Marty

This works for me....

string strText = "Text me " + Environment.NewLine + "Text me " +
Environment.NewLine;
strText = strText.Replace(Environment.NewLine, "_");

or this works..

string strText = "Text me " + Environment.NewLine + "Text me " +
Environment.NewLine;

strText = strText.Replace("\r\n", "_");

Vijay
"Marty" <xm******@hotmail.com> wrote in message
news:PzK7f.55138$Io.27031@clgrps13...
Hi,

I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');
and it doesn't work, what is missing?

Thank you!
Marty

Nov 17 '05 #3

P: n/a
Marty,

You almost had it. The Replace() method returns the modified string. Try
this...

string strMyString = "This is\r\n some line\r wrapping\n text";
strMyString = strMyString.Replace("\r\n", "_").Replace("\r",
"_").Replace("\n", "_");

John
"Marty" <xm******@hotmail.com> wrote in message
news:PzK7f.55138$Io.27031@clgrps13...
Hi,

I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');
and it doesn't work, what is missing?

Thank you!
Marty

Nov 17 '05 #4

P: n/a
I think you have to assign it to a different string, this is a little
bit differnet with the C++ string.

Marty wrote:
Hi,

I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');

and it doesn't work, what is missing?

Thank you!
Marty

Nov 17 '05 #5

P: n/a
Marty wrote:
I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');

and it doesn't work, what is missing?


Strings are immutable - String.Replace doesn't change the contents of
the string you call it on; instead, it returns a new string.

To achieve your stated aim, you should use:

strMyString = strMyString.Replace("\r\n", "_");

Note that your first attempt would replace bare carriage returns and
bare line feeds with underscores, and would replace "\r\n" with "__"
instead of "_".
Your second attempt is closer, but will only replace "\r\n" on Windows
- on other platforms, it will replace whatever the platform default
newline string is with "_".

Jon

Nov 17 '05 #6

P: n/a
Thanks guys for your help :)
Marty

Marty wrote:
Hi,

I would like to replace "\r\n" by "_" within a specific string.

I tried :
strMyString.Replace('\r', '_');
strMyString.Replace('\n', '_');
or
strMyString.Replace(System.Environment.NewLine, '_');

and it doesn't work, what is missing?

Thank you!
Marty

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.