dcsimg
Results 1 to 12 of 12

Thread: Editing a string into 2 strings

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    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?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,639

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,544

    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)

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,221

    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

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,277

    Re: Editing a string into 2 strings

    Quote Originally Posted by dday9 View Post
    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.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,544

    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    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!

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    Re: Editing a string into 2 strings

    Found the answer! its microsoft.visualbasic.left(VARIABLE, # OF LETTERS TO COUNT AND CUT) and len(VARAIBLE)

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,544

    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

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,221

    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

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,478

    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

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,277

    Re: Editing a string into 2 strings

    Quote Originally Posted by habydlg View Post
    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
  •  



Featured


Click Here to Expand Forum to Full Width