Next Letter ABCD..


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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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();



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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);
In my example, you will need to include 'using System.Text'  in the code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.