63

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!]

6 Answers 6

116

Programming 101:

char c = 'A';
//char c = 'b'; you may use lower case character.
int index = char.ToUpper(c) - 64;//index == 1
8
  • 4
    int index = ((int) char.ToUpper(c)) - 64; Nov 18, 2013 at 9:55
  • 2
    so include it in answer :) Nov 18, 2013 at 9:55
  • 6
    No need for the cast. Just do int index = char.ToUpper(c) - 'A'; Also, shouldn't A be at index 0? C# uses 0-based indices! Nov 18, 2013 at 10:04
  • @MatthewWatson - I agree on 0 if it was an array, but I do want A to be "Character 1 of the alphabet" in this particular case.
    – BlueChippy
    Nov 18, 2013 at 10:10
  • Another variant: int index = (int)c % 32;.
    – Vladimir
    Nov 18, 2013 at 10:17
72

For lower and upper case:

int index = (int)c % 32;
5
  • 6
    I had to try it out because I didn't believe it would work.. but it does, this is very nice!
    – Alex
    Nov 18, 2013 at 10:12
  • 1
    'a' is 0x41 and 'A' is 0x61. 0x20 = 32. And 26 < 32.
    – Vladimir
    Nov 18, 2013 at 10:14
  • 1
    That's clever, @Vladimir.
    – code4life
    Sep 6, 2018 at 11:10
  • 3
    @Vladimir how about vise versa? Get Char from index?
    – Luiey
    Jan 18, 2019 at 2:14
  • @Luiey What about simply return (char) ( 'a' + (position - 1)? Apr 30, 2020 at 6:25
8

A clear, readable, 0-based implementation of @Ahmed's method with bounds checking.

/// <summary>
/// Converts a latin character to the corresponding letter's index in the standard Latin alphabet
/// </summary>
/// <param name="value">An upper- or lower-case Latin character</param>
/// <returns>The 0-based index of the letter in the Latin alphabet</returns>
private static int GetIndexInAlphabet(char value)
{
    // Uses the uppercase character unicode code point. 'A' = U+0042 = 65, 'Z' = U+005A = 90
    char upper = char.ToUpper(value);
    if (upper < 'A' || upper > 'Z')
    {
        throw new ArgumentOutOfRangeException("value", "This method only accepts standard Latin characters.");
    }

    return (int)upper - (int)'A';
}
1
  • Conversion to a 1-based return value is trivial and left to the reader. ;) Nov 4, 2014 at 18:29
7

Since char and int can be mixed and matched in calculations, you can treat you char as a number (which will for sure fall between well-known values):

char c = 'A';
var index = (c < 97 ? c - 64 : c - 96);
3

Here is a nice implementation for reading columns from an Excel string to a column number. kudos to @ahmed-kraiem & @vladimir for the answer above.

   public int AddColFromLetter(string s)
    {
        int column = 0;
        int iter = 1;
        foreach (char c in s)
        {
            int index = char.ToUpper(c) - 64;//Ahmed KRAIEM
            //int index = (int)c % 32;//Valdimir
            if(iter == 1)
                column += index;
            if(iter > 1)
                column += 25+ index;
            iter++;
        }
        return column;
    }
1
  • 1
    Precisely what I wanted! Works like a charm.
    – Morvael
    Feb 8, 2019 at 15:52
1

Inspired from Vladimir's answer, another way to do this will be

int index= c & 0b11111;

Explanation: 'A' is 65 in ascii, which is 01000001 in binary. 'a' is 95 which is 01100001 in binary. We will get alphabet integer position if we simply discard 3 most significant bytes and that's what that bitmask do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.