By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

Extracting text from a string

P: n/a
Hi,
I am using VB6 and want to extract text from a string. But ONLY take out
words that begin with 't' or 'd'. The mainstring is input by the user into
'txtMain' and then by clicking a command button, all the words that begin
with 't' or 'd' will be extracted and appear in a second text box,
txtExtract.

So, i know that I have to search through the main string for the character
"d", then extract the following characters until it reaches " " (the space).
I know I will have to use the Mid function to find it and the Len function
to get extract the whole word. The loop it to do the whole string. In
theory it works, but in practice I cant seem to get it to do what I want!
If for example, I type in "dog", the program extracts "d" into the text box,
not the whole word.

Can anyone give me some advice please?

Thanks

Cassandra


Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

I'd suggest to split the words using the "Split" function and then look for
the first letter.

Here's the code:

--------

Option Explicit
Option Base 0

Private Sub Command1_Click()

Dim words() As String 'array of words
words = Split(txtMain.Text, " ") 'puts every word in the array

Dim i As Integer
For i = 0 To UBound(words) 'passes through the array
If Left(words(i), 1) = "t" Or Left(words(i), 1) = "d" Then
If Len(txtExtract.Text) = 0 Then 'adds the proper words into the
second textbox, delimiting them with spaces
txtExtract.Text = words(i)
Else
txtExtract.Text = txtExtract.Text & " " & words(i)
End If
End If
Next i

End Sub
--------

-- germol

"cassandra.flowers" <ca***************@btopenworld.com> a écrit dans le
message de news:bp**********@titan.btinternet.com...
Hi,
I am using VB6 and want to extract text from a string. But ONLY take out
words that begin with 't' or 'd'. The mainstring is input by the user into 'txtMain' and then by clicking a command button, all the words that begin
with 't' or 'd' will be extracted and appear in a second text box,
txtExtract.

So, i know that I have to search through the main string for the character
"d", then extract the following characters until it reaches " " (the space). I know I will have to use the Mid function to find it and the Len function
to get extract the whole word. The loop it to do the whole string. In
theory it works, but in practice I cant seem to get it to do what I want!
If for example, I type in "dog", the program extracts "d" into the text box, not the whole word.

Can anyone give me some advice please?

Thanks

Cassandra

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.