Home > Archive > Visual Basic > March 2006 > Extracting a string from a sentence
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Extracting a string from a sentence
|
|
|
| I was wondering if there is a command to extract a specific string from
a sentence.
Let's say, I have the following sentence:
"One two three four five six seven eight"
And I was wanting to extract the phrase "one".
I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
phrases from a sentence provided the length of the sting is known
prior. I however, would like a simple command that extracts a string
without knowing the length of the string before hand, i.e. simply be
able to extract the first string from a sentence.
Any ideas?
-d
| |
| Karl E. Peterson 2006-03-29, 6:56 pm |
| den1s wrote:
> I was wondering if there is a command to extract a specific string
> from a sentence.
>
> Let's say, I have the following sentence:
>
> "One two three four five six seven eight"
>
> And I was wanting to extract the phrase "one".
>
> I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
> phrases from a sentence provided the length of the sting is known
> prior. I however, would like a simple command that extracts a string
> without knowing the length of the string before hand, i.e. simply be
> able to extract the first string from a sentence.
>
> Any ideas?
Maybe Split()? Hard to say, unless you define "first string".
--
Working without a .NET?
http://classicvb.org/
| |
| Rick Rothstein 2006-03-29, 6:56 pm |
| >I was wondering if there is a command to extract a specific string from
> a sentence.
>
> Let's say, I have the following sentence:
>
> "One two three four five six seven eight"
>
> And I was wanting to extract the phrase "one".
>
> I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
> phrases from a sentence provided the length of the sting is known
> prior. I however, would like a simple command that extracts a string
> without knowing the length of the string before hand, i.e. simply be
> able to extract the first string from a sentence.
Your question is not worded as clearly as I would like, so I am only
guessing on this... but I think you want the InStr function. You can find
the location of a piece of text within a larger piece of text. For example,
if your larger string was
TextString = "One Two Three Four Five"
and you wanted to know where in the text the word "Three" is, you could do
this...
Position = InStr(TextString, "Three")
and that would return 9 as an answer. Note that the optional syntax for this
function **requires** that a starting location for the search be specified
(it defaults to 1 when not specified). I mention this because the use of the
InStr function in my example is case sensitive as shown; it would not find
any occurrence of the word "three". You can do this, but you need to specify
a starting position and a 4th argument, like this
Position = InStr(1, TextString, "Three", vbTextCompare)
The above line would find either "Three" or "three" (and, of course, any
capitalization that you want could have been used in its 3rd argument).
Now, as I said, I'm guessing this is what you need to use, but I am unclear
as to what you want to do afterwards.
Rick
| |
|
|
"first string"?
Do you mean that you would like to extract the first (leftmost)
word from a string composed of words seperated by a space?
strFirst = left$(strSentence, instr(strSentence," ")-1)
Saga
"den1s" <dingoboy79NON@SPAMMEgmail.com> wrote in message
news:1143670405.630999.224420@i39g2000cwa.googlegroups.com...
>I was wondering if there is a command to extract a specific string from
> a sentence.
>
> Let's say, I have the following sentence:
>
> "One two three four five six seven eight"
>
> And I was wanting to extract the phrase "one".
>
> I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
> phrases from a sentence provided the length of the sting is known
> prior. I however, would like a simple command that extracts a string
> without knowing the length of the string before hand, i.e. simply be
> able to extract the first string from a sentence.
>
> Any ideas?
>
> -d
>
| |
| George Bashore 2006-03-29, 6:56 pm |
| try this
Private Sub Form_Load()
Dim myStr As String
myStr = "One two three four five six seven eight"
MsgBox Replace(myStr, "One", "")
End Sub
"den1s" <dingoboy79@gmail.com> wrote in message
news:1143670405.630999.224420@i39g2000cwa.googlegroups.com...
>I was wondering if there is a command to extract a specific string from
> a sentence.
>
> Let's say, I have the following sentence:
>
> "One two three four five six seven eight"
>
> And I was wanting to extract the phrase "one".
>
> I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
> phrases from a sentence provided the length of the sting is known
> prior. I however, would like a simple command that extracts a string
> without knowing the length of the string before hand, i.e. simply be
> able to extract the first string from a sentence.
>
> Any ideas?
>
> -d
>
| |
|
| When you say "Extract" do you mean take it out of the sentance, or find if
it exists in the sentance?
Here's a bit of code that might help
Dim MyStr As String
Dim StrToFind As String
Dim StrPos As Integer
Dim NewStr As String
MyStr = "One two three four five six seven eight"
StrToFind = "One "
'Do it one way
StrPos = InStr(1, MyStr, StrToFind)
If StrPos > 0 Then
MsgBox "Found '" & StrToFind & "' at position " & _
StrPos & ". Rest of Sentance reads " & _
Left(MyStr, StrPos - 1) & Right(MyStr, Len(MyStr) _
- (Len(StrToFind) + StrPos - 1))
Else:
MsgBox StrToFind & " Not Found"
End If
'Do it another way
NewStr = Replace(MyStr, StrToFind, "")
MsgBox "Without '" & StrToFind & "' The Sentance reads " _
& NewStr
End Sub
"den1s" <dingoboy79@gmail.com> wrote in message
news:1143670405.630999.224420@i39g2000cwa.googlegroups.com...
>I was wondering if there is a command to extract a specific string from
> a sentence.
>
> Let's say, I have the following sentence:
>
> "One two three four five six seven eight"
>
> And I was wanting to extract the phrase "one".
>
> I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
> phrases from a sentence provided the length of the sting is known
> prior. I however, would like a simple command that extracts a string
> without knowing the length of the string before hand, i.e. simply be
> able to extract the first string from a sentence.
>
> Any ideas?
>
> -d
>
| |
|
|
"den1s" <dingoboy79@gmail.com> wrote in message
news:1143670405.630999.224420@i39g2000cwa.googlegroups.com...
>I was wondering if there is a command to extract a specific string from
> a sentence.
>
> Let's say, I have the following sentence:
>
> "One two three four five six seven eight"
Well, that's not a sentence. No verb. That's just a list.
>
> And I was wanting to extract the phrase "one".
Look up VB's InStr function.
(untested air code; I don't have VB currently installed so I can't test
this)
sText = "One two three four five six seven eight"
sTextToFind = "one"
lPos = InStr(1, sText, sTextToFind, vbTextCompare)
If lPos Then
sTextFound = Mid$(sText, lPos, Len(sTextToFind))
End If
There are still issues you'll need to deal with. For example, you could
have something like "twenty-one " in your list.
--
Mike
Microsoft MVP Visual Basic
| |
|
|
"George Bashore" <gbashore@bcpl.net> wrote in message
news:u5F2D24UGHA.2004@TK2MSFTNGP10.phx.gbl...
> try this
>
> Private Sub Form_Load()
> Dim myStr As String
> myStr = "One two three four five six seven eight"
> MsgBox Replace(myStr, "One", "")
> End Sub
>
He didn't say he wanted to replace the text. He wants to find it.
--
Mike
Microsoft MVP Visual Basic
| |
| George Bashore 2006-03-29, 9:55 pm |
| Oh, I thought he said extract it.
"MikeD" <nobody@nowhere.edu> wrote in message
news:OhkWPg5UGHA.2360@TK2MSFTNGP09.phx.gbl...
>
> "George Bashore" <gbashore@bcpl.net> wrote in message
> news:u5F2D24UGHA.2004@TK2MSFTNGP10.phx.gbl...
>
> He didn't say he wanted to replace the text. He wants to find it.
>
> --
> Mike
> Microsoft MVP Visual Basic
>
| |
| George Bashore 2006-03-29, 9:55 pm |
| http://www.answers.com/extract&r=67
"George Bashore" <gbashore@bcpl.net> wrote in message
news:uYVpM95UGHA.2492@TK2MSFTNGP11.phx.gbl...
> Oh, I thought he said extract it.
>
>
> "MikeD" <nobody@nowhere.edu> wrote in message
> news:OhkWPg5UGHA.2360@TK2MSFTNGP09.phx.gbl...
>
>
| |
| Rick Rothstein 2006-03-29, 9:55 pm |
| >> He didn't say he wanted to replace the text. He wants to find it.
> Oh, I thought he said extract it.
He did... but I'm not sure that is what he meant. Hopefully the OP will come
back and clarify things for us.
Rick
| |
| J French 2006-03-30, 3:55 am |
| On 29 Mar 2006 14:13:25 -0800, "den1s" <dingoboy79@gmail.com> wrote:
>I was wondering if there is a command to extract a specific string from
>a sentence.
>
>Let's say, I have the following sentence:
>
>"One two three four five six seven eight"
>
>And I was wanting to extract the phrase "one".
>
>I know the phrase 'Right', 'Left' and 'Mid' can be used to extract
>phrases from a sentence provided the length of the sting is known
>prior. I however, would like a simple command that extracts a string
>without knowing the length of the string before hand, i.e. simply be
>able to extract the first string from a sentence.
' ########################################
######################
'
'
Public Function StrExtStr$(StringIn$, Delimiter$, Nth%)
' mod 26/10/99 JF - Allowed Delim over 1 Byte
' 27/6/03 JF - Fixed Start
Dim QEnd&, Count&, Found&, Start&, DLen%
If Nth < 1 Then
Exit Function
End If
DLen = Len(Delimiter$)
Start = 1 - DLen ' 27/6/03 - eg: 1 -> 0
QEnd = 1 - DLen
Count = 0
Found = 0
While Found = 0
Start = QEnd + DLen
QEnd = InStr(Start, StringIn$, Delimiter$)
Count = Count + 1
If Count = Nth Or _
QEnd = 0 Then _
Found = 1
Wend
If Count = Nth Then
If QEnd = 0 Then QEnd = Len(StringIn$) + 1
StrExtStr$ = Mid$(StringIn$, Start, QEnd - Start)
End If
End Function
| |
| Jeff Johnson [MVP: VB] 2006-03-30, 6:56 pm |
|
"George Bashore" <gbashore@bcpl.net> wrote in message
news:OTVPBA6UGHA.2704@tk2msftngp13.phx.gbl...
> http://www.answers.com/extract&r=67
In programming, we generally consider "extract" to mean definition #6 in the
link above. For all others we would probably use the word "remove" or
"delete."
| |
| George Bashore 2006-03-30, 6:56 pm |
| Here is another one Jeff.
http://www.computerhope.com/extract.htm
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:OYqBwuDVGHA.1688@TK2MSFTNGP11.phx.gbl...
>
> "George Bashore" <gbashore@bcpl.net> wrote in message
> news:OTVPBA6UGHA.2704@tk2msftngp13.phx.gbl...
>
>
> In programming, we generally consider "extract" to mean definition #6 in
> the link above. For all others we would probably use the word "remove" or
> "delete."
>
| |
|
| Thanks.
This was close to what I was looking for...had to use a couple of other
commands in combination with it but your submission pretty much hit the
point.
| |
| Jeff Johnson [MVP: VB] 2006-03-31, 6:55 pm |
|
"George Bashore" <gbashore@bcpl.net> wrote in message
news:%23imgIVFVGHA.328@TK2MSFTNGP11.phx.gbl...
> Here is another one Jeff.
> http://www.computerhope.com/extract.htm
Uh, okay. And are you aware that the EXTRACT command does not REMOVE the
desired file from the .CAB? So again, "extract" is being used in the sense
of "get in a non-destructive way."
Perhaps others have had different experiences, but I personally have never
considered "extract"--in programming--to mean the same thing it does in,
say, surgery.
|
|
|
|
|