The extended version of string spilt method in C#

Posted By  pranay rana On 29 Jul 2011 10:07:47
emailbookmarkadd commentsprint
No of Views:666
Bookmarked:0 times
Votes:0 times

Introduction

In this tips,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.

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.

Split function to split string in array

String.Split( char[])

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.

Sign Up to vote for this article
Other popular Tips/Tricks
    In this tip, I will explain how to use the BigInteger in C# 4.0.The BigInteger is new data type in .NET 4.0.
    Published Date : 22/Sep/2011
    The Facebook is very popular and it has great support to other development Languages by Facebook SDKs.In this tips, I going to explain how to upload Videos to Facebook through C# with few lines of code.
    Published Date : 13/Jun/2011
    In this tip, i will share with you , how to format time HH:MM:SS from seconds in C#.
    Published Date : 25/Feb/2011
    Enums in dot net programming is a great facility and we all used it to increase code readability. In earlier version of .NET framework we don’t have any method anything that will check whether a value is assigned to it or not. In C# 4.0 we have new static method called HasFlag which will check that particular value is assigned or not.
    Published Date : 01/Jan/2011
    There are lots of features in .NET framework. In this article, you will learn how to use the Compute method, which is exists within the data table object.
    Published Date : 26/Jul/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top