我需要一些將 JS 代碼轉換為 C# 的幫助。JS代碼:
function Decription(string) {
var newString = '',
char, codeStr, firstCharCode, lastCharCode;
var ft = escape(string);
string = decodeURIComponent(ft);
for (var i = 0; i < string.length; i ) {
char = string.charCodeAt(i);
if (char > 132) {
codeStr = char.toString(10);
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) 31;
newString = String.fromCharCode(firstCharCode) String.fromCharCode(lastCharCode);
} else {
newString = string.charAt(i);
}
}
return newString;
}
我系結到 C# 上進行翻譯:
private string Decription(string encriptedText)
{
string ft = Regex.Escape(encriptedText);
string text = HttpUtility.UrlDecode(ft);
string newString = "";
for (int i = 0; i < text.Length; i )
{
var ch = (int)text[i];
if(ch > 132)
{
var codeStr = Convert.ToString(ch, 10);
var firstCharCode = Convert.ToInt32(codeStr.Substring(0, codeStr.Length - 2), 10);
var lastCharCode = Convert.ToInt32(codeStr.Substring(codeStr.Length - 2, codeStr.Length), 10) 31;
}
}
}
但是我如何翻譯這一行:
newString = String.fromCharCode(firstCharCode) String.fromCharCode(lastCharCode);
也許您知道 C# 上 String.fromCharCode() 的等效方法嗎?
uj5u.com熱心網友回復:
干得好:
public static void Main()
{
// from 97 to a
int i = 97;
Console.WriteLine(StringFromCharCode(97));
}
public static string StringFromCharCode(int code) => ((char)code).ToString();
演示
請注意,您可能希望在您的情況下使用 aStringBuilder而不是連接string。
與C# 中的 Int to Char相關(閱讀它。它包括關于 cast vs 的一個很好的評論Convert.ToChar)
uj5u.com熱心網友回復:
您可以簡單地使用Convert.ToChar方法,Convert.ToChar(97)將回傳a。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317069.html
下一篇:在PackageManagerConsole中添加遷移時,EFCoreIEntityTypeConfiguration類中未命中斷點
