Jessica Jessica - 1 month ago 7
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

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.

Comments