I'm trying to find if there is a quick way to get the integer position of a character in the alphabet (C#).
I can simply create an array and get the position, but seems there must be a "nice and funky" way of acheiving this?
I've also looked at taking the ASCII position of the (uppercase) character in relation to "65"...but again, seems more work than it should be!
[English 26 letter alphabet only, no internationalisation required - and no, this is not homework!]
To convert Python char to int, use the ord() method. The ord() is a built-in Python function that accepts a character and returns the ASCII value of that character. The ord() method returns the number representing the Unicode code of a specified character.
Way 2: Using valueOf() method of String class The method valueOf() of class String can convert various types of values to a String value. It can convert int, char, long, boolean, float, double, object, and char array to String, which can be converted to an int value by using the Integer. parseInt() method.
int position = 'g' - 'a' + 1; In C, char values are convertible to int values and take on their ASCII values. In this case, 'a' is the same as 97 and 'g' is 103. Since the alphabet is contiguous within the ASCII character set, subtracting 'a' from your value gives its relative position.
Programming 101:
char c = 'A'; //char c = 'b'; you may use lower case character. int index = char.ToUpper(c) - 64;//index == 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With