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

replace all spaces in a string with ‘ 20’ c#
write a program to replace all spaces in a string with '%20 in c
c# replace space with nothing
replace hyphen with space in java
replace newline with space in c
string space in c#
replace space with underscore java
c# replace space with character

I have strings such as:

var abc = "Menu Link";

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


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.

How To Remove White Spaces From String In Java?, How do I replace multiple spaces with a single space in C#? The .Replace(temp, " ") replaces all matches in the string temp with a space. If you want to use this multiple times, here is a better option, as it creates the regex IL at compile time: Regex singleSpacify = new Regex(" {2,}", RegexOptions.Compiled); temp = singleSpacify.Replace(temp, " ");


Using String.Replace(char,char) instead of String.Replace(string, string) should be much faster. i.e.

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

How to trim whitespace between characters, Strings are immutable, you need to do: excel = excel.Replace(' ','_');. String.​Replace() wont alter the original string, it will instead return a new  As the title says, I am attempting to replace every letter in a string with an underscore followed by a space. For example: "hello" would be replaced with "_ _ _ _ _". I can replace letters with just a space or just an underscore, but I am having trouble with replacing both. Any help is appreciated!


simply add .Replace() function at the end of the string.

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

How do I replace multiple spaces with a single space in C#?, In this program, we need to replace all the spaces present in the string with a C​. #include <stdio.h>; #include <string.h>; int main(); {; char string[] = "Once in a  I want to replace only leading and trailing white space of a string by number of underscore. Input String" New Folder " (Notes: There is one white space at front and two white spaces at the end of this string) Output. My desire output string "_New Folder__" (The output string has one underscore at the front and two underscore at the end.)


(Using zero characters in the replacement string—a pair of quotes with 'C:/​upload_test/'); // replace any spaces in original filename with underscores // at the​  Ideally, I would like to allow only a single hyphen, underscore, or space without another hyphen, underscore, or space following, e.g., "my- name", "my--name", and "my-_name" would all return false. Any help from a regular expression whiz out there is appreciated.


George Omura, Brian C. Benton If you want to change the first 3 in this string of numbers, you have to specify 3334 when prompted for the string to change. If you start with a space, as in _3334333 (we're using an underscore here only to  Replacing all punctuation in a string with spaces Posted 05-08-2017 (3631 views) In the data I list below, I'd like to replace all punctuation in variable TxtVar with a space.


Autodesk Official Press George Omura, Brian C. Benton If you want to change the first 3 in this string of numbers, you have to specify 3334 when If you start with a space, as in _3334333 (I'm using an underscore here only to represent the​  We can rewrite wasteful string Replace code with StringBuilder. Here we see the original code that uses string and then the new StringBuilder code. Version 1: This version ends up creating many string copies. It replaces whitespace around punctuation. Environment.NewLine Empty String. Version 2: MinifyWithStringBuilder is similar but uses StringBuilder. It does the same replacements as version 1.