• 0

[VB.NET] Any method similar to StringTokeniser?


Question

I want to break a single string to smaller strings on the encounter of a delimiter.

Is there any method in VB.NET that can do that?

(Something like StringTokeniser of JAVA)

Thanks.

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

You can use String.Split or Regex.Split to break a string into tokens.

public static void Example()
{
        string str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
        foreach (string s in str.Split(' ', ','))
        {
                Console.WriteLine(s);
        }
}

Link to comment
Share on other sites

  • 0

VB.NET example:

'Declarations
Dim sa As String()
Dim s As String = "text, text, text, text, TEXT, TEXT, TEXT, TEXT"
'Split the text
sa = s.Split(", ")
'Enumerate and print each string
'TODO: If you are using VS.NET 2002 remove "As String" in the following line and
'      add Dim es As String to the top
For Each es As String In sa
        Console.WriteLine(es)
Next
'Clean up
sa = Nothing
s = Nothing

Link to comment
Share on other sites

  • 0
Thanks for the replies.

I don't understand the split parameters of figgy's example, can someone explain?

String.Split can take a ParamArray of Char. He specified String.Split(" ", ",") instead of String.Split(" ,"). Got it?

Link to comment
Share on other sites

  • 0
It means it will return anything up to a space or a comma.

Wrong. See my above post. Split automatically combines Split(" ", ",") into Split(" ,").

Link to comment
Share on other sites

  • 0
Wrong. See my above post. Split automatically combines Split(" ", ",") into Split(" ,").

Well, maybe you didn't quite understand what I was saying. I was saying that when you go through each one, it will return the next token that was up to the next comma or space. It doesn't matter that Split puts them into one thing.

For example, his will print out:

Lorem

ipsum

dolor

sit

amet

consectetur

adipisicing

elit

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.