Advertisement
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. |
//C# - Calling the Split(char[ ] delimiters) method
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 + "'");
}
}
Original String is: ..Benny...and..Joon...
The delimiter values specified in a char array: .
Total number of substring after splitting : 11
''
''
'Benny'
''
''
'and'
''
'Joon'
''
''
''
//C# - Calling the Split(char[ ] delimiters, int total) method
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 + "'");
}
}
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...'
Advertisement
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 Split(String[ ], StringSplitOptions) method
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 + "'");
}
}
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'
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement