Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment character in a string

Tags:

vb.net

I have a 2 character string composed only of the 26 capital alphabet letters, 'A' through 'Z'.

We have a way of knowing the "highest" used value (e..g "IJ" in {"AB", "AC", "DD", "IH", "IJ"}). We'd like to get the "next" value ("IK" if "IJ" is the "highest").

Function GetNextValue(input As String) As String
  Dim first = input(0)
  Dim last = input(1)
  If last = "Z"c Then
    If first = "Z"c Then Return Nothing

    last = "A"c
    first++
  Else
    last++
  EndIf

  Return first & last
End Function

Obviously char++ is not valid syntax in VB.NET. C# apparently allows you to do this. Is there something shorter less ugly than this that'd increment a letter? (Note: Option Strict is on)

CChar(CInt(char)+1).ToString

Edit: As noted in comment/answers, the above line won't even compile. You can't convert from Char -> Integer at all in VB.NET.

like image 512
Jeff B Avatar asked Feb 24 '26 12:02

Jeff B


2 Answers

The tidiest so far is simply:

Dim a As Char = "a"
a = Chr(Asc(a) + 1)

This still needs handling for the "z" boundary condition though, depending on what behaviour you require.

Interestingly, converting char++ through developerfusion suggests that char += 1 should work. It doesn't. (VB.Net doesn't appear to implicitly convert from char to int16 as C# does).

To make things really nice you can do the increment in an Extension by passing the char byref. This now includes some validation and also a reset back to a:

<Extension>
Public Sub Inc(ByRef c As Char)

    'Remember if input is uppercase for later
    Dim isUpper = Char.IsUpper(c)

    'Work in lower case for ease
    c = Char.ToLower(c)

    'Check input range
    If c < "a" Or c > "z" Then Throw New ArgumentOutOfRangeException

    'Do the increment
    c = Chr(Asc(c) + 1)

    'Check not left alphabet
    If c > "z" Then c = "a"

    'Check if input was upper case
    If isUpper Then c = Char.ToUpper(c)

End Sub

Then you just need to call:

Dim a As Char = "a"        
a.Inc() 'a is now = "b"
like image 130
Jon Egerton Avatar answered Feb 27 '26 02:02

Jon Egerton


My answer will support up to 10 characters, but can easily support more.

Private Sub Test
    MsgBox(ConvertBase10ToBase26(ConvertBase26ToBase10("AA") + 1))
End Sub

Public Function ConvertBase10ToBase26(ToConvert As Integer) As String
    Dim pos As Integer = 0

    ConvertBase10ToBase26 = ""
    For pos = 10 To 0 Step -1
        If ToConvert >= (26 ^ pos) Then
            ConvertBase10ToBase26 += Chr((ToConvert \ (26 ^ pos)) + 64)
            ToConvert -= (26 ^ pos)
        End If
    Next
End Function

Public Function ConvertBase26ToBase10(ToConvert As String) As Integer
    Dim pos As Integer = 0

    ConvertBase26ToBase10 = 0
    For pos = 0 To ToConvert.Length - 1
        ConvertBase26ToBase10 += (Asc(ToConvert.Substring(pos, 1)) - 64) * (26 ^ pos)
    Next
End Function
like image 24
UnhandledExcepSean Avatar answered Feb 27 '26 01:02

UnhandledExcepSean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!