类1
public class EscapeCharacterEntities
{
private static object _lookupLocker = new object();
private static string[] _entitiesList = new string[]
{
"\n-n",
"\r-r",
"\t-t",
"\f-f",
"\v-v"
};
private static Hashtable _entitiesLoopupTable;
private EscapeCharacterEntities() { }
public static char Lookup(string entity)
{
if (_entitiesLoopupTable == null)
{
lock (_lookupLocker)
{
if (_entitiesLoopupTable == null)
{
Hashtable curTable = new Hashtable();
foreach (string item in _entitiesList)
{
curTable[item.Substring(2)] = item[0];
}
_entitiesLoopupTable = curTable;
}
}
}
object obj = _entitiesLoopupTable[entity];
return obj == null ? (char)0 : (char)obj;
}
}
{
private static object _lookupLocker = new object();
private static string[] _entitiesList = new string[]
{
"\n-n",
"\r-r",
"\t-t",
"\f-f",
"\v-v"
};
private static Hashtable _entitiesLoopupTable;
private EscapeCharacterEntities() { }
public static char Lookup(string entity)
{
if (_entitiesLoopupTable == null)
{
lock (_lookupLocker)
{
if (_entitiesLoopupTable == null)
{
Hashtable curTable = new Hashtable();
foreach (string item in _entitiesList)
{
curTable[item.Substring(2)] = item[0];
}
_entitiesLoopupTable = curTable;
}
}
}
object obj = _entitiesLoopupTable[entity];
return obj == null ? (char)0 : (char)obj;
}
}
转换方法
public static string EscapeCharacterFormat(string input)
{
StringBuilder builder = new StringBuilder();
StringWriter writer = new StringWriter(builder);
int l = input.Length;
int maxIndex = l - 1;
for (int i = 0; i < l; i++)
{
char ch = input[i];
if (ch == '\\' && i < maxIndex)
{
char ch2 = input[i + 1];
char curchar = EscapeCharacterEntities.Lookup(ch2.ToString());
if (curchar != (char)0)
{
writer.Write(curchar);
i++;
continue;
}
}
writer.Write(ch);
}
return builder.ToString();
}
{
StringBuilder builder = new StringBuilder();
StringWriter writer = new StringWriter(builder);
int l = input.Length;
int maxIndex = l - 1;
for (int i = 0; i < l; i++)
{
char ch = input[i];
if (ch == '\\' && i < maxIndex)
{
char ch2 = input[i + 1];
char curchar = EscapeCharacterEntities.Lookup(ch2.ToString());
if (curchar != (char)0)
{
writer.Write(curchar);
i++;
continue;
}
}
writer.Write(ch);
}
return builder.ToString();
}