DataColumn.Expression プロパティ (System.Data)に記されているのですが、分かりづらいのでまとめてみました。
(実際のコードでしまします)
class EscapePair
{
public EscapePair(char specialChar, string prefix, string surfix)
{
SpecialChar = specialChar;
Prefix = prefix;
Surfix = surfix;
}
public char SpecialChar;
public string Prefix;
public string Surfix;
}
// ~ ( ) # \ / = > < + - * % & | ^ ' " [ ]
private EscapePair[] EscapePairs = new EscapePair[]
{
new EscapePair('~', "[", "]"),
new EscapePair('(', "[", "]"),
new EscapePair(')', "[", "]"),
new EscapePair('#', "[", "]"),
new EscapePair('\\', "[", "]"),
new EscapePair('/', "[", "]"),
new EscapePair('=', "[", "]"),
new EscapePair('>', "[", "]"),
new EscapePair('<', "[", "]"),
new EscapePair('+', "[", "]"),
new EscapePair('-', "[", "]"),
new EscapePair('*', "[", "]"),
new EscapePair('%', "[", "]"),
new EscapePair('&', "[", "]"),
new EscapePair('|', "[", "]"),
new EscapePair('^', "[", "]"),
new EscapePair('\'', "[\'", "]"), // 注意:シングルクォーテーションは二つ並べる
new EscapePair('"', "[", "]"),
new EscapePair('[', "[", "]"),
new EscapePair(']', "[", "]"),
};
private string EscapeSpecialChar(string text)
{
StringBuilder src = new StringBuilder(text);
StringBuilder dst = new StringBuilder(text.Length * 2);
for (int i = 0; i < src.Length; i++)
{
bool escaped = false;
foreach (EscapePair ep in EscapePairs)
{
if (src[i] == ep.SpecialChar)
{
dst.Append(ep.Prefix);
dst.Append(src[i]);
dst.Append(ep.Surfix);
escaped = true;
break;
}
}
if (!escaped)
{
dst.Append(src[i]);
}
}
return dst.ToString();
}
.jpg)
|