Help with Arrays

G

Guest

I have a predefined array that looks something like

Private RemoveWords() As String = {"a", "b", "c"

and I want to take an input string, and remove the words from the string that are in the "RemoveWords" array. So basically if my string was "Walk a dog" it would return "Walk dog"

I tried doing something like
Dim tempArray() As String = inputString.Split(" "

For Each tempArray In RemoveWord
' Not sure what goes in here ye
Nex

but for the RemoveWords I get an error that says "value of type 'String' cannot be converted to '1-dimensional array of String'.

I haven't worked with arrays hardly at all, so I'm not really sure how to do this. Can anybody point me in the right direction

Thanks
Jeremy
 
J

JimM

Jeremy,

Looks like you need a nested loop. The outer loop iterates through the
values in tempArray() (each iteration containing current value in stSource).
The inner loop iterates through each value in RemoveWords() (each iteration
containing current value in stRemove).

'--------------------
' Revised loop code:
'--------------------
For Each stSource As String In tempArray
For Each stRemove As String In RemoveWords
' Within this loop, compare each stRemove (a String) value against
' value of stSource, the current word of tempArray
Next stRemove
Next stSource

Hope this helps,

--- JimM ---


Jeremy said:
I have a predefined array that looks something like:

Private RemoveWords() As String = {"a", "b", "c"}

and I want to take an input string, and remove the words from the string
that are in the "RemoveWords" array. So basically if my string was "Walk a
dog" it would return "Walk dog".
I tried doing something like:
Dim tempArray() As String = inputString.Split(" ")

For Each tempArray In RemoveWords
' Not sure what goes in here yet
Next

but for the RemoveWords I get an error that says "value of type 'String'
cannot be converted to '1-dimensional array of String'."
I haven't worked with arrays hardly at all, so I'm not really sure how to
do this. Can anybody point me in the right direction?
 
K

Kevin Spencer

The Split method of a String returns an Array of Strings. In your array of
"RemoveWords" you have strings. However, in your "For" statement, you're
assigning an array of strings to the Item in the array, which is a string.
Try:

Private RemoveWords() As String = {"a", "b", "c"}
Dim s As String
For Each s In RemoveWords
' etc
Next

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Jeremy said:
I have a predefined array that looks something like:

Private RemoveWords() As String = {"a", "b", "c"}

and I want to take an input string, and remove the words from the string
that are in the "RemoveWords" array. So basically if my string was "Walk a
dog" it would return "Walk dog".
I tried doing something like:
Dim tempArray() As String = inputString.Split(" ")

For Each tempArray In RemoveWords
' Not sure what goes in here yet
Next

but for the RemoveWords I get an error that says "value of type 'String'
cannot be converted to '1-dimensional array of String'."
I haven't worked with arrays hardly at all, so I'm not really sure how to
do this. Can anybody point me in the right direction?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top