Results 1 to 6 of 6

Thread: Simple string slice

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2008
    Posts
    268

    Simple string slice

    Hey,

    Simple question, I'm just not thinking straight right now I guess.

    Let's say you have a string:

    apple#orange, or it could be apple#grape or any other word after the # sign.

    How can I slice everything before the # sign? I can't just go Right(string, 6) because the length of the word on the right isn't always the same length.


    Thanks!

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Simple string slice

    Here is one way, but it isn't efficient:
    vbcode Code:
    1. Option Explicit
    2.  
    3. Private Sub CommandButton1_Click()
    4.  
    5.     MsgBox Slice_Text("apple#orange")
    6.     MsgBox Slice_Text("apple#grape")
    7.     MsgBox Slice_Text("pineapple#mango")
    8.  
    9. End Sub
    10.  
    11. Private Function Slice_Text(ByVal strIn As String) As String
    12.  
    13.     Slice_Text = Split(strIn, "#")(1)
    14.  
    15. End Function

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Simple string slice

    In any case you will need to do some error handlinng, at very least check for the pressence of the delimiter ("#" in this case)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2008
    Posts
    268

    Re: Simple string slice

    Thanks for the reply, Yeah I was looking for a method that would get it regardless of what is on the right hand side, since it depends on user input.

    Your above solution would only work with orange, grape and mango for example right? Only the specified terms.

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Simple string slice

    And, using Mid$() and InStr():
    vbcode Code:
    1. Option Explicit
    2.  
    3. Private Sub CommandButton1_Click()
    4.  
    5.     MsgBox Slice_Text("apple#orange")
    6.     MsgBox Slice_Text("apple#grape")
    7.     MsgBox Slice_Text("pineapple#mango")
    8.  
    9. End Sub
    10.  
    11. Private Function Slice_Text(ByVal strIn As String) As String
    12. Dim lngPos As Long
    13.  
    14.     lngPos = InStr(strIn, "#")
    15.    
    16.     Slice_Text = Mid$(strIn, lngPos + 1, Len(strIn) - lngPos)
    17.  
    18. End Function

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Simple string slice

    Quote Originally Posted by DavidNels
    Thanks for the reply, Yeah I was looking for a method that would get it regardless of what is on the right hand side, since it depends on user input.

    Your above solution would only work with orange, grape and mango for example right? Only the specified terms.
    No, it would work with any combo...... that said I posted a different way too.

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