Tokenize a string in C#
This article illustrates the different techniques to tokenize a string in C#.
The standard solution to break a delimited string into substrings is using the String.Split() method. It returns a string array containing the substrings that are delimited by the specified separator. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { String s = "Split by space"; string[] tokens = s.Split(' '); Console.WriteLine(String.Join(", ", tokens)); // Split, by, space } } |
There are overloads of the Split() method that allows you to pass StringSplitOptions. For example, the following code excludes empty substrings from the resultant string array using StringSplitOptions.RemoveEmptyEntries for the second parameter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { String s = "Split by space"; string[] tokens = s.Split(' ', StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(String.Join(", ", tokens)); // Split, by, space } } |
The separator delimits the substrings in the string. It can be a single character, a string, a character array, or a string array. For example, the following sample uses a character array to specify the delimited strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { String s = "C#: Split by,char.array"; char[] delims = new char[] { ' ', ',', '.', ':' }; string[] tokens = s.Split(delims, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(String.Join(", ", tokens)); // C#, Split, by, char, array } } |
That’s all about tokenizing a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)