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:2057
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 search text inside PDF file using a regular expression and adding hyperlinks over the matches inside their .NET applications. To find a phrase and add hyperlink over it, first pass the regular expression as a parameter to the TextFragmentAbsorber constructor and then create a TextSearchOptions object which specifies whether the regular expression is used or not. After that get the matching phrases into TextFragments and loop though the matches to
    Published Date : 25/Feb/2015
    This technical tip shows how .NET developers can determine if the source PDF file is password protected or not inside their .NET applications using Aspose.Pdf for .NET. Aspose.Pdf for .NET provides great capabilities of dealing with PDF documents. When using Document class of Aspose.Pdf namespace to open a PDF document which is password protected, we need to provide the password information as an argument to Document constructor and in case this information is not provided, an error message is
    Published Date : 28/Jan/2015
    This technical tip shows how .NET developers can access, add, update or Delete Gmail contacts inside .NET application. Aspose.Email supports working with Gmail contacts. Using the IGmailClient interface, users can retrieve contacts from a Gmail account, create new contacts, and update as well as delete existing contacts. Gmail allows developers to perform all these using its public developer's API. The following user information is required for working with Gmail contacts: User name, email addre
    Published Date : 17/Dec/2014
    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
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