Html to jsstring

public static string EncodeJsString(string s)

{

StringBuilder sb = new StringBuilder();

sb.Append("\"");

foreach (char c in s)

{

switch (c)

{

case '\"':

sb.Append("\\\"");

break;

case '\\':

sb.Append("\\\\");

break;

case '\b':

sb.Append("\\b");

break;

case '\f':

sb.Append("\\f");

break;

case '\n':

sb.Append("\\n");

break;

case '\r':

sb.Append("\\r");

break;

case '\t':

sb.Append("\\t");

break;

default:

int i = (int)c;

if (i < 32 || i > 127)

{

sb.AppendFormat("\\u{0:X04}", i);

}

else

{

sb.Append(c);

}

break;

}

}

sb.Append("\"");

return sb.ToString();

}

posted @ 2016-12-16 17:04  爱笑的3  阅读(440)  评论(0编辑  收藏  举报