0

I want to generate next character from given character. Eg : given charater = "A" next charater "B" to be generated programatically.

Preeti
  • 1,386
  • 8
  • 57
  • 112
  • 1
    What should you generate after "Z" or "z"? What should you generate after "9"? After "-", "+", "*" and so on? – Alex Zhevzhik Nov 25 '11 at 07:36
  • Alternatively, you might be looking to generate Excel style column names, in which case [this question](http://stackoverflow.com/questions/297213/translate-an-index-into-an-excel-column-name) might help. – Damien_The_Unbeliever Nov 25 '11 at 07:46

2 Answers2

1

Try this snippet:

 chr(Asc(yourCharacter)+1)
Doc Brown
  • 19,739
  • 7
  • 52
  • 88
0

You may be able to move from one code point in Unicode to the next, but that may not match the users expectations, depending on their native language, so if there's a small, limited set of characters in which you wish to work, it's probably worth just holding that as a constant string and using IndexOf to match the current character:

Private Const CharRange as String = "ABCDE"

Public Function NextChar(ByVal CurrentChar as String) as String
    Return CharRange((CharRange.IndexOf(CurrentChar(0))+1) Mod CharRange.Length)
End Function

(I'm assuming you wish to loop back to the start when the last character is reached. That assumption also may not be true)


Unicode code point version (still may not be right in all circumstances, as mentioned above):

Public Function NextChar(ByVal CurrentChar As Char) As Char
    Return Convert.ToChar(Convert.ToInt32(CurrentChar) + 1)
End Function
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448