-
Aug 21st, 2003, 09:47 PM
#1
Thread Starter
Frenzied Member
How to read a string until a certain character(s) are reached?
Hello how are you?
how can you read that line, and return everything until "a" or "ar" is reached, resulting in "Hello how "?
-
Aug 21st, 2003, 09:54 PM
#2
By using Left$ or Mid$.
VB Code:
Dim sBuff As String: sBuff = "Hello how are you?"
MsgBox Left$(sBuff, InStr(sBuff, " a") - 1)
Note: The ' ' (space) preceeding the a!
Also, there are limitations to this approach.
Experiment with Mid$ too.
Bruce.
-
Aug 21st, 2003, 10:00 PM
#3
Left$ Returns a String containing a specified number of characters from the left side of a string.
Syntax
Left$(string, length)
-
Dec 28th, 2014, 08:31 AM
#4
Addicted Member
Re: How to read a string until a certain character(s) are reached?
Edit : Wrong thread answered.
Delete this
-
Dec 28th, 2014, 12:17 PM
#5
Re: How to read a string until a certain character(s) are reached?
Originally Posted by Bruce Fox
VB Code:
Dim sBuff As String: sBuff = "Hello how are you?" MsgBox Left$(sBuff, InStr(sBuff, " a") - 1)
May not be what the OP wants, but the problem should be described better.
Does the 'a' or 'ar' need to be the start of a word, anywhere in the word, or at the end of the word: army, caring, peculiar
If looking for just 'a', what is wanted if the word was just: a
-
Dec 28th, 2014, 02:15 PM
#6
Re: How to read a string until a certain character(s) are reached?
These are my lovely routines...
You can make an xml or a html parser using these..
A$="Hello how are you?"
B$="ar"
' a$ is passing by reference so we get a changed a$
C$=GetStrUntil(b$,a$,false)
' You can split without removing "ar"
? c$, a$
If we want to drop until "ar"
A$="Hello how are you?"
B$="ar
DropLeft b$, A$
' but now we have this "e you?"
You can use "<" for GetStrUntil a tag start with removing
You can use ">" for GetStrUntil a tag start with removing
A$=html$
a1$=GetStrUntil("<",a$)
tagS1$=GetStrUntil(">",a$)
data1$=GetStrUntil("<",a$)
tagE1$=GetStrUntil(">",a$)
' dropleft used to get something after a specific string or nothing
We can move a line next or a$ will be empty..
dropleft vbcrlf , a$
Code:
Public Sub DropLeft(ByVal uStr As String, fromStr As String)
Dim i As Long
i = InStr(fromStr, uStr)
If i > 0 Then
fromStr = Mid$(fromStr, i + Len(uStr))
Else
fromStr = ""
End If
End Sub
Public Function GetStrUntil(ByVal sStr As String, fromStr As String, Optional RemoveSstr As Boolean = True) As String
Dim i As Long
If fromStr = "" Then GetStrUntil = "": Exit Function
i = InStr(fromStr, sStr)
If i < 2 Then
GetStrUntil = ""
fromStr = ""
Else
GetStrUntil = Left$(fromStr, i - 1)
If RemoveSstr Then
fromStr = Mid$(fromStr, Len(sStr) + i)
Else
fromStr = Mid$(fromStr, i)
End If
End If
End Function
-
Dec 28th, 2014, 05:17 PM
#7
Re: How to read a string until a certain character(s) are reached?
ummm you guys do know that this thread is over 10 years old, right?
-
Dec 28th, 2014, 05:19 PM
#8
Re: How to read a string until a certain character(s) are reached?
Originally Posted by DataMiser
ummm you guys do know that this thread is over 10 years old, right?
Color me embarrassed
-
Dec 28th, 2014, 05:29 PM
#9
Re: How to read a string until a certain character(s) are reached?
From his posting history it looks like a .Nutter had gotten lost in here, started to pontificate, then burnt his baby and bailed.
-
Dec 28th, 2014, 07:12 PM
#10
Re: How to read a string until a certain character(s) are reached?
@DataMiser
you are right..........
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
|