load words in a string into an array?

  • Thread starter Thread starter J
  • Start date Start date
J

J

anybody know a vb.net command to populate an array with each word in a
string? for example " hi how are you"

array(0) = hi
array(1) = how
array(2) = are
array(3) = you

i know i could loop though and search for spaces and build it that way...but
a built in command would be great.

any thoughts?
 
J
There are three Split functions in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

Seeing as you want a single space I would recommend String.Split, something
like:


Dim input As String = " hi how are you"
Dim array As String()
array = input.Split(" "c)
For Each word As String in words
Debug.WriteLine(word)
Next

Note the trailing or leading spaces will cause an empty string to be added
to the returned array. You can use String.Trim first to remove trailing or
leading spaces if needed...

Hope this helps
Jay
 
anybody know a vb.net command to populate an array with each word in a
string? for example " hi how are you"

array(0) = hi
array(1) = how
array(2) = are
array(3) = you

i know i could loop though and search for spaces and build it that way...but
a built in command would be great.

any thoughts?

Dim words() As String = "hi how are you".Split(" ".ToCharArray())
 
awesome! thanks a lot

Jay B. Harlow said:
J
There are three Split functions in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

Seeing as you want a single space I would recommend String.Split, something
like:


Dim input As String = " hi how are you"
Dim array As String()
array = input.Split(" "c)
For Each word As String in words
Debug.WriteLine(word)
Next

Note the trailing or leading spaces will cause an empty string to be added
to the returned array. You can use String.Trim first to remove trailing or
leading spaces if needed...

Hope this helps
Jay
 
* "J said:
anybody know a vb.net command to populate an array with each word in a
string? for example " hi how are you"

array(0) = hi
array(1) = how
array(2) = are
array(3) = you

i know i could loop though and search for spaces and build it that way...but
a built in command would be great.

Have a look at 'Strings.Split' or the string's 'Split' method.
 
Back
Top