Hello all,
How do I properly increment (counting) a number using the alphabet? For example,
a + 1 = b
b + 1 = c
......
z + 1 = aa
aa + 1 = ab
.......
Any help is greatly appreciated.
Printable View
Hello all,
How do I properly increment (counting) a number using the alphabet? For example,
a + 1 = b
b + 1 = c
......
z + 1 = aa
aa + 1 = ab
.......
Any help is greatly appreciated.
i would use the character values..
chr(97) = a
chr(98) = b
vb Code:
if value > 122 then ' value for z chr(97) & chr(value - 25)
i think.
edit. this is confusing me. sorry if i made it worse.
I don't know if this will help:
http://www.freevbcode.com/ShowCode.asp?ID=5440
vb.net Code:
Public Sub Numbers() Dim s As String ' String value Dim v As Integer ' Character increment Dim x As Integer = -1 ' Used for multiple characters Dim p As Integer = 97 ' Start of characters.. (97 = a) For i As Integer = 0 To 200 v = i + p If (v = 123) Then x += 1 p -= 27 Else If x > -1 Then s = Chr(97 + x) & Chr(v) Else s = Chr(v) End If Console.WriteLine(s) End If Next End Sub
edit.
works up to zz properly... zz = 728
Yeah, that's it... It gave me an idea about the logic behind... Just have to rewrite it using proper VB.Net code :)Quote:
Originally Posted by nmadd
Thank you.
You certainly helped me many times, so I'm glad I could finally help you. :bigyello:
I believe you can use the "Asc" method to get the caracter's ASCI code and than add one to it and show the total number by Chr method. Here an example:
vb Code:
Dim myAsci As Integer myAsci = Asc("a") + 1 Debug.WriteLine(Chr(myAsci))
for the "aa + 1 = ab" you use the same logic. For example the "a" letter asci is 97 and "z"'s is 121 and any thing between are other letters. You can have a flag that if the characters asci + 1 is less than 122 than return Chr(asci).
Here is the method
vb Code:
Private Function SomeThing(ByVal myString As String, ByVal num As Integer) As String Dim lastChar As Char = myString.Substring(myString.Length - 1) Dim asci As Integer = Asc(lastChar) + num If asci > 122 Then myString = myString.Substring(0, myString.Length - 1) & "aa" Else myString = myString.Substring(0, myString.Length - 1) & Chr(asci) End If Return myString End Function
Thank you all who have spent your precious time solving my question... You guys are really helpful :thumb:
To NPassero: while I have no doubt that your code would work correctly up to "zz", however I need a more robust solution - a function that takes a string a spits out the next incremented string regardless of the input length. Your solution seems to work with pre-known, fixed string length only.
To VBDT: Your function does something different than what I expect.... For example, If I plug in "zz", I expect to see the output as "aaa", not "zaa" :)
Anyway, this is the solution, in case someone else is also looking to do the same thing as I'm...
Code:Private Function IncrementString(ByVal input As String) As String
Dim output As String = String.Empty
Dim length As Integer = input.Length
Dim curChar As Char = Nothing
Dim i As Integer = 0
If String.IsNullOrEmpty(input) Then
output = "a"
Else
For i = length - 1 To 0 Step -1
curChar = input(i)
If curChar = "z"c Then
input = input.Substring(0, i) & "a" & input.Substring(i + 1)
If i = 0 Then
input = "a" & input
End If
Else
input = input.Substring(0, i) & Chr(Asc(curChar) + 1) & input.Substring(i + 1)
Exit For
End If
Next
output = input
End If
Return output
End Function
Gotcha, didn't know the functionality that you were using it for. good work.