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:1926
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 technical tip shows how .NET developers can integrate table with database (DOM) and populate data from a database using Aspose.Pdf for .NET. Databases are specially built to store and manage data in a better manner. It's a very common practice for programmers to populate different kinds of objects with data from databases. If you want to populate Table object with data from any data source using Aspose.Pdf for .NET then it is possible too. And it's not only possible but it’s very easy too.
    Published Date : 19/Nov/2014
    This technical tip explains how to .NET developers can read Microsoft Outlook message file and display its contents on the screen using Aspose.Email. This migration tips gives details how Aspose products can be used to improve your applications and free you from dependency on traditional automation. This migration tip shows how to read a Microsoft Outlook message file and display its contents on the screen using both Microsoft Office Automation and Aspose.Email code. The sample code below only
    Published Date : 22/Oct/2014
    This technical tip explains how developers can store, read & delete a Word document in Database inside their .NET Applications. One of the tasks you may need to perform when working with documents is storing and retrieving Document objects to and from a database. For example, this would be necessary if you were implementing any type of content management system. The storage of all previous versions of documents would be required to be stored in a database system. The ability to store documents
    Published Date : 03/Sep/2014
    This technical tip explains how developers can work with content controls inside a MS Word documents using .NET. In Microsoft Word, you can create a form by starting with a template and adding content controls, including check boxes, text boxes, date pickers, and drop-down lists. In Aspose.Words, a Structured Document Tag or content control from any document loaded into Aspose.Words is imported as a StructuredDocumentTag node. Structured document tags (SDT or content control) allow to embed cus
    Published Date : 20/Aug/2014
    This Technical tip explains how .NET developers can generate multiple barcodes on a single image inside their applications using Aspose.BarCode for .NET. Aspose.BarCode for .NET can efficiently create multiple optimized barcodes on a single image. It also supports generating multiple type of barcodes such as postal, QR, PDF417, EAN, Code 39, Code128, ISBN, MSI, GS1 etc. Aspose.BarCode is a .NET component for generation and recognition of Linear and 2D barcodes on all kinds of .NET applications
    Published Date : 23/Jul/2014
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
</