tokenizing strings

  • Thread starter Thread starter Christian O'Connell
  • Start date Start date
C

Christian O'Connell

Hello - in Java you have a class called java.util.StringTokenizer that
tokenizes strings, ie

StringTokenizer st=new StringTokenizer("In the beginning "," ",false);
while (st.next())
System.out.println(st.nextToken());

would print out "In", "the", and "beginning", as separate string
tokens, the string being tokenized on the " " (space) character. Is
there a similar utility class in VB.Net.

Chris
 
Look up the \*Split\* method in the String class.
Regards
Hexathioorthooxalate
 
* (e-mail address removed) (Christian O'Connell) scripsit:
Hello - in Java you have a class called java.util.StringTokenizer that
tokenizes strings, ie

StringTokenizer st=new StringTokenizer("In the beginning "," ",false);
while (st.next())
System.out.println(st.nextToken());

would print out "In", "the", and "beginning", as separate string
tokens, the string being tokenized on the " " (space) character. Is
there a similar utility class in VB.Net.

In your case you can use 'Strings.Split' or the string's 'Split' method.
 
Christian,
In addition to the others comments:

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.

If you are using C#, you can reference the Microsoft.VisualBasic.dll
assembly to use the first function.

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

Dim input as String = "In the beginning "
Dim words() as String
words = input.Split(" "c)
For Each word As String in words
Debug.WriteLine(word)
Next

Note the trailing space will cause an empty string to be added to the return
value. You can use String.Trim to remove trailing spaces if needed...

Hope this helps
Jay
 
Back
Top