The Split
method in the string
class is used
to divide a string into an array of substrings based on specified delimiters(seperator).
It can be overloaded to handle various splitting scenarios, such as using single
or multiple delimiters, specifying options to remove empty
entries, and limiting the number of substrings.
Split Method
accepts min1 and max 3 parameters
Separator (delimiter)
- Mandatory param - char or string
Count – optional param – no of separator
occurrence to split
StringSplitOptions - optional param
but useful – to remove empty entries and trim
end space of each word
Split string by single char
String Courses=”C# Language , Java Language, Python
Language, Go Language,”;
String[] CourseList= Courses.Split(‘,’);
Result : [“C# Language ”,” Java Language” ,” Python Language”,” Go
Language”,””]
Split string by single char & Remove Empty entries
String[] CourseList= Courses.Split(‘,’, StringSplitOptions.RemoveEmptyEntries);
Result : [“C# Language ”,” Java Language” ,” Python Language”,” Go
Language”]
Split string by Multiple characters
String Course3 =” C# , Java,Python , Go_Angular
React@Javascript-NextJs ”;
String[] CourseList= Course3.Split(new char[]{‘ ’ , ’-’ ,
‘@’ , ‘-’});
Result : [“C#” , “Java” , “Python” , ” Go” , “ Angular” , “ React” ,
“Javascript” , “NextJs”]
Split string by single word
String Courses4=”C# Language
Java Language Python Language Go Language ”;
String[] CourseList= Courses.Split(“Language”);
Result : [“C#”,” Java” ,” Python”,” Go”]
Split String by Collection of words or string array
string input = "C# Language Java Core Python Script GoLang Language"; // split
collection word
string[] outputArr = input.Split(new string[] { "Language", "Core", "Script" });
Result : [“C#”,” Java” ,” Python”,” GoLang”]
Split String by Collection of string and char not
available but you can use characters also in the form of string value
string input = "C# Language Java Core Python Script GoLang Language Angular , React@Javascript-NextJs ";
collection word
string[] outputArr = input.Split(new string[] { "Language", "Core", "Script",” ” , “-“ , “@” , “-“ });
Result : [“C#”,” Java” ,” Python”,” GoLang”,”React”,”Javascript”,”NextJs”]
No comments:
Post a Comment
Thanks for the contribution, our team will check and reply back if response required.