c sharp

C# string split

In C#, a substring that is a part of the string is formed in many ways. For instance, we use a built-in function of strings that is the substring() itself. There are different ways to create a substring. One of them is by using a split method of string. This is also a built-in feature.

Split Function()

This function helps to make substrings through the separation of the string at a specific point. These points are the part of the string; they are selected as delimiters or separators. The separators can be any character or an array of characters. In other words, those symbols that will divide a single string into two strings is a separator. The split() function generates an array of strings that are created by splitting an original input string (substring).

# Split ( separator, count, StringSplitOptions options);

Parameters of the split function

  • Separator: It separates the parts of a string by generating substrings.
  • Count: It is used to control how many strings will be generated. It is an optional argument. A split function can be used without this parameter.
  • Options: This is also an optional parameter. It is used when you want to omit empty elements in the array. If you want to include the space, the value of this argument is taken as None.

Implementation of the Split() Function

Example 1

The concept of string splitting is used in C sharp by defining the libraries and classes inside it. A string data type variable is initialized with a string having characters of alphabets and symbols. The split string functions in such a way that it makes the string in parts depending on the condition we apply. For example, we declared a string with a double colon sign after each word in the whole sentence.

# String text = "I :: am :: a :: content :: writer";

We will use this string in the split function to split the string at the colon point so that the string is divided into all possible parts.

# String [ ] result = text.split("::");

The string type variable that will store the result is taken as an array because before splitting, we are unaware of the parts that may form after partition. The function of the split is called through the object of the string created first and inside the parameter, that symbol is passed to make sure the partitioning at that particular point. This symbol is a double colon.

After the split process, we will display the contents present inside the string array. A ‘for each’ loop is used to display each word in the result array.

# Foreach (string str in result);

Each line will contain each word separately after using the split function. Save this code in the text file with the extension of C sharp (‘.cs’). In Linux, we need an MCS compiler for the compilation of any .cs file. After the code is compiled, we will execute it through mono by using the file with the ‘.exe’ extension to make the file executable.

$ MCS file.cs

$ mono file.exe

On execution, you can see that each line will display a single word of characters that were separated by the double colon. The split function will not display the point at which it is applied. For example, you can see that the resultant values do not contain the colon symbol in them.

Not only has a single symbol, but the split function also works on multiple symbols at a time. It depends on the user’s choice of having the symbol or an alphanumeric value for the string to get partitioned using the character. Following the same approach of variable declaration, we used a different string along with the number of separators.

# String text = "C sharp, is.an / interesting*language";

We used ‘,’, ‘/’, ‘*’, full stop, and a blank space as separators so that the string is divided at these points. The split function will contain all of the symbols as a parameter.

# String [ ] result = text.split( ' ' , ',' , '.' , '/' , '*');

The result will be the same as the previous example. Each word is separated through the symbols we mentioned.

Example 2

Unlike the symbol separation, now we use the word characters to separate the portion of strings from each other.

A simple string will be assigned to the string type variable. This time to highlight the separators, we will use a string type array to take the separator’s values in it. Then these string separators are used as a parameter inside the split function. In this example, we have taken two separators ‘for’ and ‘c’. These separators will split the string into two parts.

# String [] result = text .split ( separators, stringSplitOptions.None);

Again foreach loop will be used to display the items of the array. On the execution, we will see that two strings are created without those two words that acted like separators.

Using separators, whether they are symbols or other characters in the string, are part of the string unless you declare their separators. Then by using the split function, they are no more part of the string.

Example 3

It is not necessary to split the string only by using the separators directly in the functions, and these separators cause the division randomly. We can manage the split function to divide the string through separators by giving the number to separate according to the number provided.

This is referred to as the ‘count’ parameter. So we declared a string here that is a mixture of symbols and alphabets. But as separators, we selected only two symbols here. ‘:’ and ‘!’.

We created a maximum of three sub-strings. This function will take the separators and the number of substrings you want to create.

# String [ ] result = text.split (separators, 3, stringsplitoptions.None);

Use the loop for displaying the elements of the string array. You can see that according to these two separators, three substrings are created. Each substring is separated through the two symbols we provided, while other characters are displayed as part of the string. In this way, we can filter the use of separators in the strings to get split.

Exceptions Occurring in Split() Function

While using this function, there are chances of some exceptions regarding the parameters.

If the count value is negative, then the error of ArgumentOutOfRangeException occurs. If the optional parameter does not contain the stringSplitOptions value, then an error of ArgumentExcpetion occurs.

Conclusion

A string is divided into parts or substrings by splitting it into parts. This is done by a split function. This function is accomplished by specifying the characters by which we will separate the string, these are the separators. These separators are essential parameters for the string split function. We explained the split function working in different ways through the symbols, words, and the count number as parameters. To avoid the exceptions in the resultant value, the count value must be positive.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.