Click here to Skip to main content
15,886,840 members
Articles / Multimedia / GDI
Technical Blog

String Split Utility

Rate me:
Please Sign up or sign in to vote.
1.40/5 (3 votes)
25 Jul 2011CPOL1 min read 17.4K   4   1
Note: Split function has more no of overload method but the below two I found useful. You may found other overloads helpful in your code.In this post I am going to discuss about two important thing about Split function of String class.
Note: Split function has more no of overload method but the below two I found useful. You may found other overloads helpful in your code.

In this post I am going to discuss about two important thing about Split function of String class. Split function of the string class split the string in array of string.

Split function to split string in array
String.Split( char[])
For example
string words = "stringa,stringb, ,stringc,stringd stringe.";
string [] split = words.Split(new Char [] {' ', ','}); 
Above code create a string array which has
//output
split[0]=stringa
split[1]=stringb
split[2]=
split[3]=stringc
split[4]=stringd
split[5]=stringe
but What If I want to remove empty string from the array when I split string.
Solution to this problem is to make use of second overload method of the the string Split where you can specify the option to remove string. So above code is rewritten as

Overload method with option
String.Split(Char[], StringSplitOptions)
string words = "stringa,stringb, ,stringc,stringd stringe.";
string [] split = words.Split(new Char [] {' ', ','},StringSplitOptions.RemoveEmptyEntries); 
Created string array is
//output 
split[0]=stringa
split[1]=stringb
split[2]=stringc
split[3]=stringd
split[4]=stringe

Now consider case where I have to limit no of return string. Consider for example
string a = "key:mykey, Value : test1,test2";  
Now I have to get the key:mykey in string 1 and Value : test1,test2 in string 2.
Overload function to split string in limited no. of string
Split(Char[], Int32)
So the code for this is
string a = "key:mykey, Value : test1,test2";
string [] split = words.Split(new Char [] {','},2);   
Now the split array have
//output
split[0]= "key:mykey";
split[1]= "Value : test1,test2";
Summary
There are also other variable of Split method which you can refer form the msdn link :String.Split. But I fond above two more useful than others.
This article was originally posted at http://pranayamr.blogspot.com/2011/07/string-split.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
GeneralMy vote of 1 Pin
Yet Another XCoder22-Aug-11 1:30
Yet Another XCoder22-Aug-11 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.