Here is an example on how to remove specific (unicode) characters in a String using C# and VB.NET

Samples

C# Sample

static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.Unicode;
            const string unicodeText = @" Ё Ђ Ѓ п р с т у ф х ц ч ";
            const string pattern = @"\u0444";
            const string patternMulti = @"\u0444|\u0401"; 
            string result = String.Empty;

            Console.WriteLine("### SINGLE REPLACE ####");
            result = Regex.Replace(unicodeText, pattern, "HELLO", RegexOptions.Compiled);
            Console.WriteLine(string.Format("Text before: {0}",unicodeText));
            Console.WriteLine(string.Format("Text after: {0}", result));
            Console.WriteLine("### MULTI REPLACE ####");
            result = Regex.Replace(unicodeText, patternMulti, "HELLO", RegexOptions.Compiled);
            Console.WriteLine(string.Format("Text before: {0}", unicodeText));
            Console.WriteLine(string.Format("Text after: {0}", result));
            
            Console.Read();
        }

VB.NET Sample

Sub Main(args As String())
             Console.OutputEncoding = System.Text.Encoding.Unicode
             Const  unicodeText As String = " Ё Ђ Ѓ п р с т у ф х ц ч "
             Const  pattern As String = "\u0444"
             Const  patternMulti As String = "\u0444|\u0401"
             Dim result As String = [String].Empty

             Console.WriteLine("### SINGLE REPLACE ####")
             result = Regex.Replace(unicodeText, pattern, "HELLO", RegexOptions.Compiled)
             Console.WriteLine(String.Format("Text before: {0}", unicodeText))
             Console.WriteLine(String.Format("Text after: {0}", result))
             Console.WriteLine("### MULTI REPLACE ####")
             result = Regex.Replace(unicodeText, patternMulti, "HELLO", RegexOptions.Compiled)
             Console.WriteLine(String.Format("Text before: {0}", unicodeText))
             Console.WriteLine(String.Format("Text after: {0}", result))

             Console.Read()
       End Sub

Result

RegexReplaceUnicodeChars

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

Related links:

Leave a Reply