How to Replace All the Spaces with %20 in C#

Efficient way to remove ALL whitespace from String?

This is fastest way I know of, even though you said you didn't want to use regular expressions:

Regex.Replace(XML, @"\s+", "");

Crediting @hypehuman in the comments, if you plan to do this more than once, create and store a Regex instance. This will save the overhead of constructing it every time, which is more expensive than you might think.

private static readonly Regex sWhitespace = new Regex(@"\s+");
public static string ReplaceWhitespace(string input, string replacement)
{
return sWhitespace.Replace(input, replacement);
}

Replace all the spaces with ;

String.Replace() don't change content it returns value. It should be:

content = content.Replace(" ", ";");

How do I replace multiple spaces with a single space in C#?

string sentence = "This is a sentence with multiple    spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
sentence = regex.Replace(sentence, " ");

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

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.

Replace string spaces with an underscore

Strings are immutable, you need to do:

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

String.Replace() wont alter the original string, it will instead return a new altered string.

String.Replace(): Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.

Remove and replace whitespace with a character in a string

Strings are immutable in .NET. You need to assign result of Replace method to some variable to use it.

var myString = "Text with spaces";
myString = myString.Replace(' ', '_');

How do I replace all the spaces with %20 in C#?

I believe you're looking for HttpServerUtility.UrlEncode.

System.Web.HttpUtility.UrlEncode(string url)

Replacing Space by Empty Character in c#

Because you are using the Replace(char, char) overload instead of Replace(string, string) ('' is not a valid character). Just use this:

string ouputString = inputString.Replace(" ", "");

How to remove all whitespace characters from a String?

Try using Linq in order to filter out white spaces:

  using System.Linq;

...

string source = "abc \t def\r\n789";
string result = string.Concat(source.Where(c => !char.IsWhiteSpace(c)));

Console.WriteLine(result);

Outcome:

abcdef789

Regex for replacing multiple white spaces with multiple  

Use a match evaluator to build the custom replacement:

var s = " 1  2   3    4 ";
var result = Regex.Replace(s, @"\s{2,}", m =>
string.Concat(Enumerable.Repeat(" ", m.Length)));
Console.WriteLine(result);
// => ' 1  2   3    4 '

See the C# demo

Here, \s{2,} matches 2 or more whitespaces, the match is assigned to m inside the match evaluator, and string.Concat(Enumerable.Repeat(" ", m.Length)) builds a string that consists of   substrings of match length times (see this thread).



Related Topics



Leave a reply



Submit