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:1672
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
    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
    This technical tip shows how to .NET Developers can add text in an existing PDF file using Aspose.Pdf. Developers can add simple or underline text as well as can add border around added text in an existing PDF document. To add text to an existing PDF file Open the input PDF using the Document object and get the particular page to which you want to add the text. After that create a TextFragment object with the input text along with other text properties. The TextBuilder object created from that p
    Published Date : 26/Nov/2013
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
</