467,140 Members | 1,362 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,140 developers. It's quick & easy.

Extracting text from a string

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
  • viewed: 8817
Share:
1 Reply
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.

Similar topics

4 posts views Thread by lecichy | last post: by
7 posts views Thread by Tempo | last post: by
6 posts views Thread by sunil | last post: by
4 posts views Thread by dexter48 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.