iron2000 Posted August 14, 2004 Share Posted August 14, 2004 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 More sharing options...
0 figgy Posted August 14, 2004 Share Posted August 14, 2004 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 More sharing options...
0 itsnotabigtruck Posted August 14, 2004 Share Posted August 14, 2004 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 More sharing options...
0 iron2000 Posted August 15, 2004 Author Share Posted August 15, 2004 Thanks for the replies. I don't understand the split parameters of figgy's example, can someone explain? Link to comment Share on other sites More sharing options...
0 itsnotabigtruck Posted August 15, 2004 Share Posted August 15, 2004 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 More sharing options...
0 kjordan2001 Posted August 15, 2004 Share Posted August 15, 2004 Thanks for the replies.I don't understand the split parameters of figgy's example, can someone explain? It means it will return anything up to a space or a comma. Link to comment Share on other sites More sharing options...
0 iron2000 Posted August 15, 2004 Author Share Posted August 15, 2004 Understood. Thanks. Anyway I've just started on VB.NET and learning things I need as I go. Link to comment Share on other sites More sharing options...
0 itsnotabigtruck Posted August 15, 2004 Share Posted August 15, 2004 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 More sharing options...
0 kjordan2001 Posted August 15, 2004 Share Posted August 15, 2004 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 More sharing options...
0 figgy Posted August 15, 2004 Share Posted August 15, 2004 (edited) Edited out. Edited August 15, 2004 by figgy Link to comment Share on other sites More sharing options...
Question
iron2000
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