Editing a string into 2 strings
Greetings again,
Here's the plot...
I must input a letter and a number in a text box.. say "A14", and them I want to cut the string into "A" and "14" and then use these, but I do not know how to code this as the next string could be "A1" or "C110" and these change... could I make an if for each one? like an if for when its 2 characters and just cut it in half and then an if when its 3 characters and cut between the 1st and second and so on?:confused:
Re: Editing a string into 2 strings
Well first we'll have to establish what variations are expected. Will it always begin with 1 letter and the rest are numbers ? Or could it have more than 1 letter ? Or would it have numbers coming first at anytime ?
We have to establish that first before we continue.
Re: Editing a string into 2 strings
Assuming that the pair would be only 1 letter and x amount of numbers, something like this may work:
Code:
Dim letter As String
Dim number As Integer
letter = TextBox1.Text.Substring(0, 1)
number = Integer.Parse(TextBox1.Text.Substring(1, CInt(TextBox1.TextLength - 1)))
Label1.Text = letter
Label2.Text = CStr(number)
Re: Editing a string into 2 strings
I'd do something like that, too, except that I wouldn't use Parse. I just don't trust the user. In this case especially, the chance of a typo causing invalid data is way too high.
Re: Editing a string into 2 strings
Quote:
Originally Posted by
dday9
Assuming that the pair would be only 1 letter and x amount of numbers, something like this may work:
Code:
Dim letter As String
Dim number As Integer
letter = TextBox1.Text.Substring(0, 1)
number = Integer.Parse(TextBox1.Text.Substring(1, CInt(TextBox1.TextLength - 1)))
Label1.Text = letter
Label2.Text = CStr(number)
When sending the SubString to the Integer.Parse(), you don't have to specify the length of the string to parse, you can do this:
Code:
number = Integer.Parse(TextBox1.Text.Substring(1))
And it will return from index 1 until the end of the string automatically.
Also, I would use TryParse() instead of Parse() because the user's input could still be invalid.
Re: Editing a string into 2 strings
For Shaggy :]
Code:
Dim letter As String
Dim number As Integer
letter = TextBox1.Text.Substring(0, 1)
If Integer.TryParse(TextBox1.Text.Substring(1, TextBox1.TextLength - 1), number) = True Then
Label1.Text = letter
Label2.Text = number.ToString
Else
MessageBox.Show("Please enter the correct format.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Edit - And for JuggaloBrotha as well, woot woot!
Edit #2 -
Quote:
When sending the SubString to the Integer.Parse(), you don't have to specify the length of the string to parse,
I never knew that, I allways typed out the exact length. That would've saved me some trouble a while back :P
Re: Editing a string into 2 strings
The first one will always be a letter and there will be a maximun of 3 numbers after it. No other format. Thanks in advance!
Re: Editing a string into 2 strings
Found the answer! its microsoft.visualbasic.left(VARIABLE, # OF LETTERS TO COUNT AND CUT) and len(VARAIBLE)
Re: Editing a string into 2 strings
Quote:
microsoft.visualbasic.left(VARIABLE, # OF LETTERS TO COUNT AND CUT) and len(VARAIBLE)
Ouch, you should defenatly atleast look at my code in post #6; mine's more of a .net way
Re: Editing a string into 2 strings
Yeah. That's not much of an answer. It's all VB6. While the code is still around in .NET, it has been replaced by better things.
Re: Editing a string into 2 strings
If using VS2010 and want to trap for improper format try the following
Code:
Public Function GetParts(ByVal sender As String) As Tuple(Of String, Integer)
Dim ResultString As String = ""
Dim ResultInt As Integer = 0
If sender.Length > 0 Then
If Char.IsLetter(sender(0)) Then
ResultString = sender.ToUpper.Substring(0, 1)
Dim temp As String = sender.Substring(1).TrimEnd
If temp.Length <> 3 Then
Throw New Exception("Invalid len for integer part")
End If
If Not Integer.TryParse(temp, ResultInt) Then
Throw New Exception("Invalid format - integer part missing")
End If
Else
Throw New Exception("Invalid format - first char must be a-z or A-Z")
End If
Else
Throw New Exception("Invalid format")
End If
Return Tuple.Create(Of String, Integer)(ResultString, ResultInt)
End Function
Re: Editing a string into 2 strings
Quote:
Originally Posted by
habydlg
Found the answer! its microsoft.visualbasic.left(VARIABLE, # OF LETTERS TO COUNT AND CUT) and len(VARAIBLE)
One of the worst possible ways to handle this....
I suggest you re-read this: Post 6