How to replace Html breaks with newline in C# and VB.NET

To replace Html breaks with newline in C# and VB.NET you can use the following snippet.

Sample C#

public static string HtmlBreaksToNewline(string input)
{
	return input.Replace("<br />", "\r\n");
}

public static string NewlineToHtmlBreaks(string input)
{
	return input.Replace("\r\n", "<br />");
}

Sample VB.NET

Public Shared Function HtmlBreaksToNewline(input As String) As String
	Return input.Replace("<br />", vbCr & vbLf)
End Function

Public Shared Function NewlineToHtmlBreaks(input As String) As String
	Return input.Replace(vbCr & vbLf, "<br />")
End Function

How to remove unicode characters from a string in C# and VB.NET

There are various methods to remove unicode characters from a String in .NET.
Below i will show you some methods and the benchmark results.
Before choosing a method, take a look at the Benchmark result and the Framework Compatibility.

Benchmark Summary

A for Loop removed 100 000 times the unicode characters of the string value
ᾭHeὣlݬl♫oѪ₪ Wor♀ld. This has been repeated 40 Times for each method. All Methods returned the right result Hello World.

Method Average runtime (ms)
Regex 2 433 204
Regex (compiled) 1 646 337
String Normalization 1 016 305
Encodings 2 183 387
LINQ 492 708

 

Methods

Remove Unicode Characters using Regex

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

 

Remove Unicode Characters using Regex (Compiled)

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

 

Remove Unicode Characters using String Normalization

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

 

Remove Unicode Characters using Encodings

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

 

Remove Unicode Characters using LINQ

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

Do you have an alternate or faster method for removing unicode?

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

How to change the href for a hyperlink using jQuery

To change the href for a hyperlink using jQuery you can use the following snippet.
The following example will change every href to http://codesnippets.fesslersoft.de.

Sample jQuery

$("a").attr("href", "http://codesnippets.fesslersoft.de/")

if you want to change every href pointing to http://codesnippets.fesslersoft.de with a href pointing to http://codesnippets.fesslersoft.de/change-the-href-for-a-hyperlink you can use the following snippet.

Sample jQuery

$("a[href^='http://codesnippets.fesslersoft.de/']")
   .each(function()
   { 
      this.href = this.href.replace("http://codesnippets.fesslersoft.de/", "http://codesnippets.fesslersoft.de/change-the-href-for-a-hyperlink");
   });

How to replace multiple spaces with a single space in c# or vb.net

To replace multiple spaces with a single space in c# or vb.net you can use a Regular Expression

Sample c#

var inputString = "This  is    a Text with    too much     spaces   in   it!!!";
inputString = Regex.Replace(inputString, @"[ ]{2,}", @" ", RegexOptions.None);

Sample VB.NET

Dim inputString = "This  is    a Text with    too much     spaces   in   it!!!"
inputString = Regex.Replace(inputString, "[ ]{2,}", " ", RegexOptions.None)

there are more ways to do that, but i personally prefer the regex way.
A other way to archieve that would be to use a loop. The snippet below is replacing double spaces until there are no double spaces left.

 

Sample c#

var inputString = "This  is    a Text with    too much     spaces   in   it!!!";
while (inputString.Contains(@"  "))
{
    inputString = inputString.Replace(@"  ", @" ");
}

Sample VB.NET

Dim inputString = "This  is    a Text with    too much     spaces   in   it!!!"
While inputString.Contains("  ")
	inputString = inputString.Replace("  ", " ")
End While

playing with the RegexOptions can speed up or slow down the regex snippet a bit, see RegexOptions Enumeration in the MSDN

How to replace a line break in C# and VB.NET

Replacing a line break is a simple challenge. In .NET there are plenty of ways to do that.
You can for example use String.Replace or Regex.Replace. My prefered method is String.Replace but both versions will do it.

String.Replace Method for c#

sampleString = sampleString.Replace(System.Environment.NewLine, "replacement string");

String.Replace Method for vb.net

sampleString = sampleString.Replace(System.Environment.NewLine, "replacement string")

Regex.Replace Method for c#

sampleString = Regex.Replace(sampleString, @"\r\n?|\n", "replacement string");

Regex.Replace Method for c#

sampleString = Regex.Replace(sampleString, "\r\n?|\n", "replacement string")

more informations can be found in the msdn Regex.Replace, String.Replace