Hi,
Please refer below code.
HTML
<div>
<asp:TextBox ID="txtAlphabet" runat="server" />
<asp:Button Text="Increament" runat="server" OnClick="Increament" />
<br />
<asp:Label ID="lblResult" runat="server" />
</div>
C#
protected void Increament(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtAlphabet.Text))
{
lblResult.Text = increament(txtAlphabet.Text);
}
}
public string increament(string number)
{
number = number.Trim();
int result;
bool isNumber = int.TryParse(number, out result);
if (isNumber)
{
result = result + 1;
return result.ToString();
}
else
{
if (number.Length == 1)
{
char aplpha = number[0];
if (aplpha == 'z')
{
aplpha = 'a';
}
else if (aplpha == 'Z')
{
aplpha = 'A';
}
else
{
aplpha++;
}
return aplpha.ToString();
}
else
{
int index = -1;
foreach (Match match in Regex.Matches(number, "[a-zA-Z]"))
{
index = match.Index;
}
string aphabet = number.Substring(index, 1);
string next = string.Empty;
int nextNum = default(int);
if (index != number.Length - 1)
{
next = number.Substring(index + 1, number.Length - 1 - index);
nextNum = Convert.ToInt32(next);
}
char aplpha = aphabet[0];
if (nextNum >= 9 || (nextNum == 0 && index == number.Length - 1))
{
if (aplpha == 'z')
{
aplpha = 'a';
}
else if (aplpha == 'Z')
{
aplpha = 'A';
}
else
{
aplpha++;
}
nextNum = 0;
}
else
{
nextNum++;
}
return index != number.Length - 1 ? string.Format("{0}{1}{2}", number.Substring(0, index), aplpha, nextNum) : string.Format("{0}{1}", number.Substring(0, index), aplpha);
}
}
}
VB
Protected Sub Increament(sender As Object, e As EventArgs)
If Not String.IsNullOrEmpty(txtAlphabet.Text) Then
lblResult.Text = increament(txtAlphabet.Text)
End If
End Sub
Public Function increament(number As String) As String
number = number.Trim()
Dim result As Integer
Dim isNumber As Boolean = Integer.TryParse(number, result)
If isNumber Then
result = result + 1
Return result.ToString()
Else
If number.Length = 1 Then
Dim aplpha As Char = number(0)
If aplpha = "z"C Then
aplpha = "a"C
ElseIf aplpha = "Z"C Then
aplpha = "A"C
Else
aplpha += 1
End If
Return aplpha.ToString()
Else
Dim index As Integer = -1
For Each match As Match In Regex.Matches(number, "[a-zA-Z]")
index = match.Index
Next
Dim aphabet As String = number.Substring(index, 1)
Dim [next] As String = String.Empty
Dim nextNum As Integer = 0
If index <> number.Length - 1 Then
[next] = number.Substring(index + 1, number.Length - 1 - index)
nextNum = Convert.ToInt32([next])
End If
Dim aplpha As Char = aphabet(0)
If nextNum >= 9 OrElse (nextNum = 0 AndAlso index = number.Length - 1) Then
If aplpha = "z"C Then
aplpha = "a"C
ElseIf aplpha = "Z"C Then
aplpha = "A"C
Else
aplpha += 1
End If
nextNum = 0
Else
nextNum += 1
End If
Return If(index <> number.Length - 1, String.Format("{0}{1}{2}", number.Substring(0, index), aplpha, nextNum), String.Format("{0}{1}", number.Substring(0, index), aplpha))
End If
End If
End Function
Screenshot