|
-
Nov 27th, 2008, 02:46 AM
#1
Thread Starter
Hyperactive Member
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!
-
Nov 27th, 2008, 02:59 AM
#2
Re: Simple string slice
Here is one way, but it isn't efficient:
vbcode Code:
Option Explicit
Private Sub CommandButton1_Click()
MsgBox Slice_Text("apple#orange")
MsgBox Slice_Text("apple#grape")
MsgBox Slice_Text("pineapple#mango")
End Sub
Private Function Slice_Text(ByVal strIn As String) As String
Slice_Text = Split(strIn, "#")(1)
End Function
-
Nov 27th, 2008, 03:03 AM
#3
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)
-
Nov 27th, 2008, 03:05 AM
#4
Thread Starter
Hyperactive Member
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.
-
Nov 27th, 2008, 03:09 AM
#5
Re: Simple string slice
And, using Mid$() and InStr():
vbcode Code:
Option Explicit
Private Sub CommandButton1_Click()
MsgBox Slice_Text("apple#orange")
MsgBox Slice_Text("apple#grape")
MsgBox Slice_Text("pineapple#mango")
End Sub
Private Function Slice_Text(ByVal strIn As String) As String
Dim lngPos As Long
lngPos = InStr(strIn, "#")
Slice_Text = Mid$(strIn, lngPos + 1, Len(strIn) - lngPos)
End Function
-
Nov 27th, 2008, 03:10 AM
#6
Re: Simple string slice
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|