您的位置:首页 > Web前端 > JavaScript

C#后台向前台注册js时,进行换行的检查

2011-04-02 14:06 204 查看
C#后台向前台注册js时,进行换行的检查

public string Enquote(string strVal)
{
if (strVal == null || strVal.Length == 0)
{
return "";
}
char c;
int i;
int len = strVal.Length;
StringBuilder sb = new StringBuilder(len + 4);
string t;
for (i = 0; i < len; i += 1)
{
c = strVal[i];
switch (c)
{
case '"':
if ((i > 0 && strVal[i - 1] != '//') || (i == 0))
{
sb.Append('//');
}
sb.Append(c);
break;
case '/b':
if ((i > 0 && strVal[i - 1] != '//') || i == 0)
{
sb.Append("//b");
}
else
{
sb.Append("b");
}
break;
case '/t':
if ((i > 0 && strVal[i - 1] != '//') || i == 0)
{
sb.Append("//t");
}
else
{
sb.Append("t");
}
break;
case '/n':
if ((i > 0 && strVal[i - 1] != '//') || i == 0)
{
sb.Append("//n");
}
else
{
sb.Append("n");
}
break;
case '/f':
if ((i > 0 && strVal[i - 1] != '//') || i == 0)
{
sb.Append("//f");
}
else
{
sb.Append("f");
}
break;
case '/r':
if ((i > 0 && strVal[i - 1] != '//') || i == 0)
{
sb.Append("//r");
}
else
{
sb.Append("r");
}
break;
default:
if (c < ' ')
{
//t = "000" + Integer.toHexString(c);
string tmp = new string(c, 1);
t = "000" + int.Parse(tmp,System.Globalization.NumberStyles.HexNumber);
sb.Append("//u" + t.Substring(t.Length - 4));
}
else
{
sb.Append(c);
}
break;
}
}
return sb.ToString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: