[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 865
  • Last Modified:

Next Letter ABCD..

Hi,

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?
0
ziwez0
Asked:
ziwez0
  • 2
1 Solution
 
Fernando SotoCommented:
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();
            MessageBox.Show(strNextLetter);

Regards,

Fernando
0
 
OBonioCommented:
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];
            else
                return Char.MinValue;
        }

Open in new window

0
 
Jammer59Commented:
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
}
else
{
      if (strCurrentLetter == "z")
      {
            // Do something special here
      }
      else
      {
              // 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);
      }
}
0
 
Jammer59Commented:
In my example, you will need to include 'using System.Text'  in the code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now