strings and sequential numbers

mykurasa

New member
Joined
Aug 22, 2007
Messages
2
Programming Experience
1-3
need help in removing the first characters in a number then adding 1 to the number then returning the 2 first characters that were removed. example i want to remove CR FROM CR499 THEN i add 1 to 499 then add back the CR.
any help will be appreciated
 
Is that a database key? If so then you really should be using a numerical field as the key and have that value as an extra field. That way you've already got the number 499 so you simply add 1 to that and that's your new key. You then prepend "CR" and put that value in another column. Now there's no messy removing of characters.

If that's not possible for some reason (you really should try to implement that if at all possible) then this will do the job:
VB.NET:
Dim str As String = "CR499"
Dim digits As Char() = "0123456789".ToCharArray()
Dim firstDigitIndex As Integer = str.IndexOfAny(digits)
Dim prefix As String = str.Substring(0, firstDigitIndex)
Dim suffix As String = str.Substring(firstDigitIndex)
Dim number As Integer

If Integer.TryParse(suffix, number) Then
    number += 1
    suffix = number.ToString()
End If

str = prefix & suffix
MessageBox.Show(str)
Note that that will remove any leading zeroes on the numerical part.
 
Back
Top