【问题标题】:What is the charCodeAt() equivalent in C#?C# 中的 charCodeAt() 等价物是什么?
【发布时间】:2014-03-21 05:22:06
【问题描述】:

C# 中的 charCodeAt() 等价物是什么?

当前代码:

   string outString="";
    for (var i = 0; i < inpString.Length; i++)
    {
    outString += inpString.charCodeAt(i).toString(); //i need to replaced charCodeAt(i) with equalent c# code
    }

如何在 C# 中做到这一点?

【问题讨论】:

    标签: c# javascript asp.net c#-4.0


    【解决方案1】:

    试试这个

    string outString="";
    
    for (var i = 0; i < inpString.Length; i++)
    
    {
    
    outString+= ((int)inpString[i]).ToString();
    
    }
    

    【讨论】:

      【解决方案2】:

      以下将返回 3,因为索引从 0 开始的第四个字符。

          string result = "012345";
          Response.Write(result.ToCharArray()[3]); ;
      

      所以你的情况

      string outString="";
      for (var i = 0; i < inpString.Length; i++)
      {
      outString += inpString.ToCharArray()[3]; //i need to replaced charCodeAt(i) with equalent c# code
      }
      

      【讨论】:

        【解决方案3】:

        如果您想获取字符串中指定字符的 unicode 字符,您可能需要查看Convert.toUint16(char)Convert.toUint32(char) 方法。

        foreach(var a in inputstring)
            Console.Write("{0} ", Convert.ToUInt16(a).ToString("X4"))
        

        【讨论】:

          猜你喜欢
          • 2014-05-08
          • 2023-03-30
          • 2010-09-17
          • 2011-07-26
          • 2011-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多