|
-
Jun 25th, 2012, 12:26 PM
#1
Thread Starter
Junior Member
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?
-
Jun 25th, 2012, 12:33 PM
#2
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.
-
Jun 25th, 2012, 12:41 PM
#3
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)
-
Jun 25th, 2012, 12:56 PM
#4
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.
My usual boring signature: Nothing
 
-
Jun 25th, 2012, 01:05 PM
#5
Re: Editing a string into 2 strings
 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.
-
Jun 25th, 2012, 01:07 PM
#6
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 -
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
Last edited by dday9; Jun 25th, 2012 at 01:11 PM.
-
Jun 26th, 2012, 12:13 AM
#7
Thread Starter
Junior Member
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!
-
Jun 27th, 2012, 01:05 PM
#8
Thread Starter
Junior Member
Re: Editing a string into 2 strings
Found the answer! its microsoft.visualbasic.left(VARIABLE, # OF LETTERS TO COUNT AND CUT) and len(VARAIBLE)
-
Jun 27th, 2012, 01:13 PM
#9
Re: Editing a string into 2 strings
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
-
Jun 27th, 2012, 02:07 PM
#10
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.
My usual boring signature: Nothing
 
-
Jun 27th, 2012, 02:10 PM
#11
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
-
Jun 27th, 2012, 02:10 PM
#12
Re: Editing a string into 2 strings
 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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|