Next Letter ABCD..

Posted on 2008-11-12
Last Modified: 2012-08-14

can someone  please give me an example of how to go to the next letter...
Example if i passed a varaible of A
string strNextLetter = B

or if I passed a W
string strNextLetter = X

I dont really want to do a case statement as there must be a easier way?
Question by:ziwez0
    LVL 62

    Accepted Solution

    Hello ziwez0,

    Code like this will do what you need.

                Char[] charLetter = textBox1.Text.Substring(0, 1).ToCharArray();
                String strNextLetter = ((Char)(charLetter[0] + 1)).ToString();


    LVL 8

    Expert Comment

    You could use an array.

            private const char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }
            private char GetNextLetter(char letter)
                int charIndex = Array.IndexOf<char>(alphabet, letter);
                if(charIndex>-1 && charIndex <= 26) // if we've passed a valid char
                    return alphabet[charIndex+1];
                    return Char.MinValue;

    Open in new window

    LVL 6

    Expert Comment

    Without going into a lot of detail, here is a simple way to do it.  Of course, you will need to determine what you want to do when the Starting Character is "Z" or "z".

    string strCurrentLetter = "B";
    string strNextLetter = "";
    string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    if (strCurrentLetter == "Z")
    // Do something special here
          if (strCurrentLetter == "z")
                // Do something special here
                  // Find the position of the Current Letter within the Alphabet
                  int intPos = Alphabet.IndexOf(strCurrentLetter);
                  // Find the character at the next position
                 strNextLetter = Alphabet.Substring(intPos + 1,1);
    LVL 6

    Expert Comment

    In my example, you will need to include 'using System.Text'  in the code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now