Results 1 to 9 of 9

Thread: [RESOLVED] string manipulation help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Philippines
    Posts
    177

    Resolved [RESOLVED] string manipulation help

    sample string: "123456 - Popo"

    i want to know on how to get my final result like this:
    variable1 = "123456"
    variable2 = "Popo"

    thanks!

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: string manipulation help

    VB Code:
    1. 'You can use the split function:
    2. Dim strTest As String
    3. Dim strSplits() As String
    4. strTest = "123456 - Popo"
    5. strSplits = Split(strTest)
    6. Debug.Print strSplits(0)
    7. Debug.Print strSplits(2)
    CS

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Philippines
    Posts
    177

    Re: string manipulation help

    any other way?

  4. #4
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: string manipulation help

    VB Code:
    1. 'Another way:
    2. Dim strTest As String
    3. Dim intPos As Integer
    4. strTest = "123456 - Popo"
    5. intPos = InStr(1, strTest, " ")
    6. 'Debug.Print intPos
    7. Debug.Print Left$(strTest, intPos - 1)
    8.  
    9. intPos = InStrRev(strTest, " ")
    10. 'Debug.Print intPos
    11. Debug.Print Right$(strTest, Len(strTest) - intPos)
    CS

  5. #5
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: string manipulation help

    VB Code:
    1. strArray = "123456-Popo"
    2.     strArray = Split(strArray, "-")
    3.     Variable1 = strArray(0)
    4.     Variable2 = strArray(1)

    OR

    VB Code:
    1. strArray = "123456-Popo"
    2.     strArray = Split("-" & strArray, "-")
    3.     Variable1 = strArray(1)
    4.     Variable2 = strArray(2)

    OR


    VB Code:
    1. strArray = "123456-Popo"
    2.     strArray = Split("-" & strArray, "-")
    3.     For e = 1 to UBound(strArray)
    4.          Variable(e) = strArray(e)
    5.     Next e

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: string manipulation help

    There are multiple ways of parsing so here one:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim sText As String
    3. Dim res1$, res2$
    4.  
    5.     sText = "123456 - Popo"
    6.     res1 = Trim(Left(sText, InStr(1, sText, "-") - 1))
    7.     res2 = Trim(Mid(sText, InStr(1, sText, "-") + 1))
    8.    
    9.     Debug.Print res1
    10.     Debug.Print res2
    11.  
    12. End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Philippines
    Posts
    177

    Re: string manipulation help

    thanks a lot for the help guys!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Location
    Philippines
    Posts
    177

    Re: string manipulation help

    Quote Originally Posted by RhinoBull
    There are multiple ways of parsing so here one:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim sText As String
    3. Dim res1$, res2$
    4.  
    5.     sText = "123456 - Popo"
    6.     res1 = Trim(Left(sText, InStr(1, sText, "-") - 1))
    7.     res2 = Trim(Mid(sText, InStr(1, sText, "-") + 1))
    8.    
    9.     Debug.Print res1
    10.     Debug.Print res2
    11.  
    12. End Sub
    i got an error in this with this example string "123456 - Popo-Cost"
    result1 = "123456 - Popo"
    result2 = "Cost"

    the result must be
    result1 = "123456"
    result2 = "Popo-Cost"

    cheers

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: [RESOLVED] string manipulation help

    If you want consistent output you have to have consistent input. Or, the old saying - garbage in, garbage out.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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