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:1789
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 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
    This technical tip shows how to extract text from part of an image inside .NET Applications. Aspose.OCR for .NET provides OcrEngine class to extract text from a specific part of the image document. The OcrEngine class requires Source image, Language and Resource file for character recognition. The source image is the document on which OCR will be performed. The image can be a BMP, TIFF, JPEG, GIF or PNG file. The OcrEngine.Image property is used to set the source image. One or more languages mus
    Published Date : 16/Jul/2014
    This technical tip explains how to create & send an email message with voting options inside .NET Applications. Microsoft Outlook allows users to create a poll when composed a new message. This is done by including voting options such as Yes, No, Maybe, etc. The FollowUpOptions class offered by Aspose.Email, provides the VotingButtons property that can be used to set or get the value of the voting options. This article provides a detailed example of creating a MapiMessage with voting options for
    Published Date : 04/Jun/2014
    This technical tip explains how .NET developers can create RE (Reply/Reply All) and FW (Forward) messages from a source message inside their .NET applications using Aspose.Email for .NET. IEWSClient lets developers create RE (Reply/Reply All) and FW (Forward) messages from a source message. The source message is identified by selecting a particular ExchangeMessageInfo from ExchangeMessageInfoCollection obtained by IEWSClient.ListMessages(). The other argument is the actual MailMessage to be sent
    Published Date : 14/May/2014
    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
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