user3432064
user3432064

Reputation: 115

What is the charCodeAt() equivalent in C#?

What is the charCodeAt() equivalent in C#?

Current Code:

   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
    }

How can i do this in C#?

Upvotes: 8

Views: 13072

Answers (3)

LakshmiNarayanan
LakshmiNarayanan

Reputation: 1188

If you want to get the unicode character at the specified character in a string, you might want to check out Convert.toUint16(char), Convert.toUint32(char) methods.

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

Upvotes: 0

A J
A J

Reputation: 2140

The following will return 3 the fourth character since index starts at 0.

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

So in your case

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

Upvotes: 0

Kailas
Kailas

Reputation: 449

Try out this

string outString="";

for (var i = 0; i < inpString.Length; i++)

{

outString+= ((int)inpString[i]).ToString();

}

Upvotes: 9

Related Questions