Advertisement



< Prev
Next >



C# String Split() Method



In C#, The Split() method of the String class is used to split the value of the invoked String object into substrings, by using the delimiter values that are specified by the elements of a String array or a character array.

These split parts i.e. substrings are returned by the Split() method by containing them in a String array, where each split part is individually a String object.




Some versions of Split() method


Methods Description
String[ ] Split(char[ ] delimiters)

It returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a char[ ] array.


String[ ] Split(char[ ] delimiters, int total)

It returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a char[ ] array.

The second parameter total represents the maximum number of substrings to return.


String[ ] Split(char[ ] delimiters, StringSplitOptions)

It returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a char[ ] array.

You can specify whether the substrings include empty array elements by specifying fields of StringSplitOptions Enum.


String[ ] Split(String[ ] delimiters, StringSplitOptions)

It returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a String[ ] array.

You can specify whether the substrings include empty array elements by specifying fields of StringSplitOptions Enum.


String[ ] Split(String[ ] delimiters, int total, StringSplitOptions)

It returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a String[ ] array.

The second parameter total represents the maximum number of substrings to return and you can specify whether the substrings include empty array elements by specifying fields of StringSplitOptions Enum.







How the Split() method splits the value of an invoked String


Let us understand the rules on how the Split() method performs the splitting of an invoked String object value to make its substrings: Don't worry, if you have not got the rules yet, let us see an example to make this easier.



Example of String[ ] Split(char[ ]) method


Let us show you an example of an overload version of Split() method i.e. Split(char[ ]), which returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a char[ ] array.

//C# - Calling the String[ ] Split(char[ ] delimiters)

using System;

class StringSplit
{
public static void Main(String[] ar)
{
	//Creating a String object
	String str1= "..Benny...and..Joon...";

	
	//Printing a String object
	Console.WriteLine("Original String is: " + str1);

	
	//Creating a char array to specify the delimiter char values
	char[] chArr = {'.'};

	//Printing the delimiter values	
	Console.Write("The delimiter values specified in a char array: ");
	foreach(char ch in chArr)
		Console.WriteLine(ch);
	
	//Splitting the value in a String object with the delimiter char values
	//Specified in a char array, chArr
	String[] substrings = str1.Split(chArr);



	//Extracting and printing the substrings in a String array
	Console.WriteLine("Total number of substring after splitting : " + substrings.Length);
	foreach(String str2 in substrings)
		Console.WriteLine("'" + str2 + "'");
}
}


Output is :


Original String is: ..Benny...and..Joon...
The delimiter values specified in a char array: .
Total number of substring after splitting : 11
''
''
'Benny'
''
''
'and'
''
'Joon'
''
''
''


Program Analysis


By following the above mentioned rules on our program, we have split the value in a String object value "..Benny...and..Joon..." into 11 parts by using a single delimiter value . specified in a char[ ] array.

Out of 11 substrings you see in the output, many are empty String values, here's the explanation behind them:




Example of String[ ] Split(char[ ], int total) method


Let us show you an example of an overload version of Split() method i.e. Split(char[ ], int total), which returns a String array, which contains the substrings by splitting the invoked String object, based on the delimiter values that are specified in a char[ ] array.

The second parameter total represents the maximum number of substrings to return.

//C# - Calling the String[ ] Split(char[ ] delimiters, int total)

using System;

class StringSplit
{
public static void Main(String[] ar)
{
	//Creating a String object
	String str1= "..Benny...and..Joon...";

	
	//Printing a String object
	Console.WriteLine("Original String is: " + str1);

	
	//Creating a char array to specify the delimiter char values
	char[] chArr = {'.', 'n'};

	//Printing the delimiter values	
	Console.Write("The delimiter values specified in a char array: ");
	foreach(char ch in chArr)
		Console.WriteLine(ch);
	
	//Splitting the value in the invoked String object into 5 substrings with the delimiter char values
	//Specified in a char array, chArr
	String[] substrings = str1.Split(chArr, 5);


	//Extracting and printing the substrings in a String array
	Console.WriteLine("Total number of substring after splitting : " + substrings.Length);
	foreach(String str2 in substrings)
		Console.WriteLine("'" + str2 + "'");
}
}


Output is :


Original String is: ..Benny...and..Joon...
The delimiter values specified in a char array: .
n
Total number of substring after splitting : 5
''
''
'Be'
''
'y...and..Joon...'


Program Analysis


By calling the Split(char[], int) version, we have split the String object value "..Benny...and..Joon..." into 5 parts by using the delimiter value . and n, which are specified in a char[ ] array.


Advertisement




Example of Split(String[ ], StringSplitOptions) method


Let us show you an example of an overload version of Split() method i.e. Split(String[ ], StringSplitOptions), which returns a String array, which contains substrings by splitting the invoked String object, based on the delimiter values that are specified in a String[ ] array.

You can specify whether the substrings include empty array elements by specifying fields of StringSplitOptions Enum. Its all important 2 fields are explained below.


Fields of StringSplitOptions Value Description
None 0
The return value includes array elements that contain an empty string.

RemoveEmptyEntries 1
The return value does not include the array elements that contain an empty string.





//C# - Calling the String[ ] Split(String[ ] delimiters)

using System;

class StringSplit
{
public static void Main(String[] ar)
{
	//Creating a String object
	String str1= "Foooo and boo go to doooo and moo";

	
	//Printing a String object
	Console.WriteLine("Original String is: " + str1);

	
	//Creating a String array to specify the delimiter String values
	//which specifies a delimiter value of space 
	String[] strArr = {"oo"};

	//Printing the delimiter values	
	Console.Write("The delimiter values specified in a String array: ");
	foreach(String str in strArr)
		Console.WriteLine(str);
	
	//Splitting the value in a String object with the delimiter String values
	//Specified in a String array, strArr
	//based on the StringSplitOptions.None, which doesn't remove the empty String substrings.
	String[] substrings = str1.Split(strArr, StringSplitOptions.None);



	//Extracting and printing the substrings in a String array
	Console.WriteLine("Total number of substring after splitting: " + substrings.Length);
	foreach(String str2 in substrings)
		Console.WriteLine("'" + str2 + "'");


	//Splitting the value in a String object with the delimiter String values
	//Specified in a String array, strArr
	//based on the StringSplitOptions.RemoveEmptyEntries, which removes the empty String substrings.
	substrings = str1.Split(strArr, StringSplitOptions.RemoveEmptyEntries);



	//Extracting and printing the substrings in a String array
	Console.WriteLine("Total number of substrings after removing the empty substrings: " + substrings.Length);
	foreach(String str2 in substrings)
		Console.WriteLine("'" + str2 + "'");
}
}


Output is :


Original String is: Foooo and boo go to doooo and moo
The delimiter values specified in a String array: oo
Total number of substring after splitting: 7
'F'
''
' and b'
' go to d'
''
' and m'
''
Total number of substrings after removing the empty substrings: 4
'F'
' and b'
' go to d'
' and m'


Program Analysis







Please share this article -





< Prev
Next >
< C# Equals() Method
C# Remove() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement