Jeff B
Jeff B

Reputation: 9002

Increment character in a string

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.

Upvotes: 3

Views: 14465

Answers (5)

sneha sp
sneha sp

Reputation: 1

Dim N as String = ""

Dim chArray As Char = Convert.ToChar(N)

    Dim a As String = CChar(Char.ConvertFromUtf32(Char.ConvertToUtf32(chArray, 0) + 1))

Upvotes: -1

rory.ap
rory.ap

Reputation: 35328

Unfortunately, there's no easy way -- even CChar(CInt(char)+1).ToString doesn't work. It's even uglier:

CChar(Char.ConvertFromUtf32(Char.ConvertToUtf32(myCharacter, 0) + 1))

but of course you could always put that in a function with a short name or, like Jon E. pointed out, an extension method.

Upvotes: 1

Jon Egerton
Jon Egerton

Reputation: 41589

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"

Upvotes: 10

UnhandledExcepSean
UnhandledExcepSean

Reputation: 12804

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

Upvotes: 1

dbasnett
dbasnett

Reputation: 11773

Try this

Private Function IncBy1(input As String) As String
    Static ltrs As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim first As Integer = ltrs.IndexOf(input(0))
    Dim last As Integer = ltrs.IndexOf(input(1))
    last += 1
    If last = ltrs.Length Then
        last = 0
        first += 1
    End If
    If first = ltrs.Length Then Return Nothing
    Return ltrs(first) & ltrs(last)
End Function

This DOES assume that the code is only two chars, and are A-Z only.

Upvotes: 0

Related Questions