asp.net the string to & #区码位编码, or & #区码位编码字符串转为对应的字符串内容.
& #数字; This encoding is actually to convert a single character to the corresponding area code bit (number), and then the area code bit prefix plus "&#", suffix plus ";" , the browser automatically resolves to the corresponding character for this encoded string.
asp.net string and & #编码转换源代码和测试代码如下:
Using System;
Using System.Text.RegularExpressions; Public partial class Purchase_property:System.Web.UI.Page {///<summary>///asp.net converts a string into a 16-code-bit & #编码/// </summary>///<param name= "S" > the string to be encoded in the 16-bit code </param>///<returns> encoded 16 code bit & #字符串 </returns> public string Stringtounicodecodebit (string s) {if (string). IsNullOrEmpty (s) | |
S.trim () = = "") return "";
string r = ""; for (int i = 0; i < s.length i++) R = = "&#" + ((int) s[i]).
ToString () + ";";
return R; The public string Rematchevaluator (Match m) {return (char) int. Parse (M.groups[1]. Value)).
ToString (); ///<summary>///asp.net converts a 16-bit & #编码转为对应的字符串///</summary>///<param name= "s" >16 into a code-bit encoded string
</param>///<returns>16 code-bit encoded string corresponding to string </returns> public string unicodecodebittostring (string s) {if (string. IsNullOrEmpty (s) | |
S.trim () = = "") return ""; Regex rx = new Regex (@ "&# (\d+);", Regexoptions.compiled); Return RX.
Replace (S, rematchevaluator);
} protected void Page_Load (object sender, EventArgs e) {string s = "asp.net region code bit string";
s = stringtounicodecodebit (s);//Convert to & #编码 Response.Write (s);
Response.Write ("\ n");
s = unicodecodebittostring (s);//& #编码转为字符串 Response.Write (s); }
}
The JavaScript version can refer to the following:
function Uncode (str) {//& #编码转换成字符 return
str.replace (/&# x)? ( [^&] {1,5});? /g, function (A, B, c) {return
String.fromCharCode (parseint (c, B, 16:10))
;} function encode (str) {//convert character to & #编码
var a = [], i = 0;
for (; i < str.length;) A[i] = Str.charcodeat (i++);
Return "&#" + a.join ("; &#") + ";";
}