Results 1 to 9 of 9

Thread: String split (Resolved)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved String split (Resolved)

    Hey All,

    How do you split the following text from a text box...

    MyText, andsomemoretext

    ...and put the second part into text2 textbox?

    I'm using VB5, and cannot use the Split function.

    Thanks in advance,
    Ron
    Last edited by rdcody; Jun 30th, 2005 at 12:20 PM.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: String split

    Use the InStr function to check for the instance of the dilimiter.
    Then, is InStr returns greater than 0 use the Left$() or Right$() string functions to get all the text before (or after depending on which one you use).

    And that should do it

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: String split

    try something like this.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strFirstString As String
    3.  
    4.     strFirstString = "MyText, andsomemoretext"
    5.  
    6.     Dim x As String
    7.     Dim y As String
    8.  
    9.     x = Left$(strFirstString, InStr(1, strFirstString, ",") - 1)
    10.     y = Trim$(Mid$(strFirstString, InStr(1, strFirstString, ",") + 1, _
    11.         (Len(strFirstString) - (InStr(1, strFirstString, ",")))))
    12.  
    13.     text1.Text = x
    14.     text2.Text = y
    15.  
    16. End Sub

    Hope this helps!!

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: String split

    You can use the split command like tihs:

    VB Code:
    1. Text2.Text = Split(Text1, ",")(1)
    2. Text1.Text = Split(Text1, ",")(0)

    Pradeep

    EDIT: The above code is assuming that your Text1 contains the string you want to split.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: String split

    One more way (using split and control arrays).

    -Place a textbox on the form, name it Text1, set its index property to 0.
    -Place another texbox and name it Text2.
    -Place a command button and name it Command1.

    -Paste the code in the form

    -Run the application
    -Enter the text (delimited with commas) in Text2
    -Press the command button
    VB Code:
    1. Option Explicit
    2.    
    3.     Private Sub Command1_Click()
    4.         Dim strBuffer()     As String
    5.         Dim I               As Long
    6.        
    7.         strBuffer = Split(Text2.Text, ",")
    8.        
    9.         For I = LBound(strBuffer) To UBound(strBuffer)
    10.             If I <> 0 Then
    11.                 Load Text1(I)
    12.                 Text1(I).Text = Trim$(strBuffer(I))
    13.                 Text1(I).Top = Text1(I - 1).Top + Text1(I).Height
    14.                 Text1(I).Visible = True
    15.             Else
    16.                 Text1(I).Text = Trim$(strBuffer(I))
    17.             End If
    18.         Next I
    19.     End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: String split

    Thanks for the replies fellers.

    The solution that Bombdrop provided worked very well. The input of
    sciguyryan explained it to me, and the solutions of baja_yu, and Pradeep1210
    use the Split function which I can't use because I'm using VB5.

    Thanks to all of you for your help!

  7. #7
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: String split (Resolved)

    Here is one that does it without the split function so you can use it. It works with more than two items.
    VB Code:
    1. Option Explicit
    2.    
    3.     Private Sub Command1_Click()
    4.         Dim strBuffer       As String
    5.         Dim intPass         As Long
    6.        
    7.         intPass = 0
    8.         Do
    9.             If InStr(Text2.Text, ",") > 0 Then
    10.                 strBuffer = Left$(Text2.Text, InStr(Text2.Text, ",") - 1)
    11.                 Text2.Text = Right$(Text2.Text, Len(Text2.Text) - InStr(Text2.Text, ","))
    12.             Else
    13.                 strBuffer = Text2.Text
    14.                 Text2.Text = ""
    15.             End If
    16.            
    17.             If intPass <> 0 Then
    18.                 Load Text1(intPass)
    19.                 Text1(intPass).Text = Trim$(strBuffer)
    20.                 Text1(intPass).Top = Text1(intPass - 1).Top + Text1(intPass).Height
    21.                 Text1(intPass).Visible = True
    22.             Else
    23.                 Text1(intPass).Text = Trim$(strBuffer)
    24.             End If
    25.            
    26.             intPass = intPass + 1
    27.         Loop Until InStr(Text2.Text, ",") = 0
    28.        
    29.         If Len(Text2.Text) > 0 Then
    30.             Load Text1(intPass)
    31.             Text1(intPass).Text = Trim$(Text2.Text)
    32.             Text1(intPass).Top = Text1(intPass).Top + Text1(intPass).Height
    33.             Text1(intPass).Visible = True
    34.             Text2.Text = ""
    35.         End If
    36.     End Sub

  8. #8
    Lively Member Rhodian's Avatar
    Join Date
    Dec 2002
    Location
    Greece- Rhodes
    Posts
    67

    Re: String split (Resolved)

    It works Fine men

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String split (Resolved)

    There is a Split function for VB5 in Codebank, and also on MSDN Knowledge Base there is a page with Split and Replace functions for VB5.

    Here's one by abhijit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width