Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Replace?!?

Status
Not open for further replies.

MattWoberts

Programmer
Oct 12, 2001
156
GB
Hi,

I need to do a really simple replace on a string. I need to replace all carriage-returns (ascii code 13) with "/n".

In VB, I would do this:

s = Replace(s,chr(13),"/n")

But I have no idea how to do this in c#! I am aware of the string.Replace method, but how I search for chr(13)!?!?!

Any help greatly appreciated!
 
You're going to kick yourself -- look at the Replace method on the String class.

Chip H.
 
It is very simple. You need to use String.IndexOf
Be aware that writing:
str.replace... is not good enough.
You have to write:
str = str.replace...
because the replace only returns a new string and doesn't change the characters of the string itself
 
Thanks I have it working now.

Its interesting that to get a newline in a windows forms control \n is not enough - you need \r\n. Anyone know the reason for this?

 
The reason is Bill Gates!!
Microsoft copied all ideas from unix and Mac, but they changed several things like the \r\n (if you create a file in unix using pico or emacs, and move it by ftp to windows, you will find out that all you text is in one line), or the backslash in path.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top