Jessica Jessica - 1 year ago 81
C# Question

How can I replace a space in a string with an underscore in C#?

I have strings such as:

var abc = "Menu Link";

Is there a simple way I can change the space to an underscore?

Answer Source

If you want to do it in place:

abc = abc.Replace(" ", "_");

Although do realize a new string instance will be created; it's not actually done in the same memory location - String is an immutable type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download