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:1679
Bookmarked:0 times
Votes:0 times


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[])


string words = "stringa,stringb, ,stringc,stringd stringe.";
string [] split = words.Split(new Char [] {' ', ','}); 

Above code create a string array which has


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


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

split[0]= "key:mykey";
split[1]= "Value : test1,test2";


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
    This Tutorial shows how .NET developers can create normal and scatter charts inside their .NET Applications. Aspose.Slides for .NET lets developers add custom charts into slides from scratch. This topic, explains how to create normal and scatter charts with multiple series from scratch using Aspose.Slides for .NET. Aspose.Slides for .NET works independently of Aspose.Cells for .NET for chart creation. Aspose.Slides for .NET has provided the simplest API for creating charts in an easy way. For C#
    Published Date : 23/Apr/2014
    This tutorial explains how .NET developers can export DXF Drawings to PDF using Aspose.Imaging for .NET. Aspose.Imaging includes a feature for reading AutoCAD DXF drawing entities and rendering them as an entire drawing into PDF format. It is possible to customize the conversion process to achieve specific results. We have introduced a new Cad class in the Aspose.Imaging.FileFormats namespace. The Cad class is a family of AutoCAD drawings and since they all share similar schemes, it was decided
    Published Date : 02/Apr/2014
    This technical tip explains how developers can use Gmail Calendars to add, retrieve delete, move or update Appointments in their own applications using Aspose.Email. Aspose.Email provides features for working with Appointments in Google calendars. Some important tasks such as adding Appointments, Retrieving list of appointments, retrieving particular appointment, updating an appointment, moving appointment from one calendar to another and deleting appointment can be performed using Google cal
    Published Date : 05/Mar/2014
    This technical tip shows how developers can split worksheets of an Excel workbook to a separate workbook, TIFF, PNG or any supported image format in the cloud. This example allows you to split all or specific worksheets of a workbook file and save each worksheet as a new workbook, TIFF or any supported image format using Aspose.Cells for Cloud API in your applications. You can use our REST API with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. Some of the code sample
    Published Date : 11/Feb/2014
    This technical tip explains how to read email messages from a Microsoft SharePoint document library. To access files in a SharePoint document library, the SharePoint SDK must be installed on the system. The SDK provides the API necessary to log in and access files from the document library. The programming samples below assume that a Microsoft Outlook Message file (.msg) is already stored in the SharedDocument folder of the SharePoint Document Library. The SharePoint SDK is used to get the messa
    Published Date : 17/Dec/2013
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
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