Encode Control Characters - CSharp System

CSharp examples for System:Char

Description

Encode Control Characters

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w w  w  .j  a v  a  2 s  .  c  o  m*/

public class Main{
        #endregion

		#region XML/SQL Server Escaping
		static string EncodeControlCharacters(string input)
		{
			int i = 0, len = input.Length;
			for (; i < len; i++)
			{
				char c = input[i];
				if (c < 0x20 && (c != 0x9 && c != 0xA && c != 0xD))
				{
					break;
				}
			}
			if (i == len)
			{
				return input;
			}
			StringBuilder sb = new StringBuilder();
			if (i != 0) { sb.Append(input, 0, i); }
			for (; i < len; i++)
			{
				char c = input[i];
				if (c < 0x20 && (c != 0x9 && c != 0xA && c != 0xD))
				{
					sb.Append((char) (0xE000+c));
				}
				else
				{
					sb.Append(c);
				}
			}
			return sb.ToString();
		}
}

Related Tutorials