Friday, May 22, 2009

To Extract Unique/All Sub-Strings from the string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace PubishApps
{
class FindSubStringRegex
{

/************************************************
* Topic : To Extract Unique/All Sub-Strings from the string
* Reference Required: System.Text.RegularExpressions.
* Author : kalit sikka
* For : http://eggheadcafe.com
* **********************************************/


///
/// To Extract Unique/All Sub-Strings from the string
///

///
///
///
///

public Match[] FindSubstrings(string source, string matchPattern,
bool findAllUnique)
{
SortedList uniqueMatches = new SortedList( );
Match[] retArray = null;

Regex RE = new Regex(matchPattern, RegexOptions.Multiline);
MatchCollection theMatches = RE.Matches(source);

if (findAllUnique)
{
for (int counter = 0; counter < theMatches.Count; counter++)
{
if (!uniqueMatches.ContainsKey(theMatches[counter].Value))
{

uniqueMatches.Add(theMatches[counter].Value,
theMatches[counter]);
}
}
retArray = new Match[uniqueMatches.Count];
uniqueMatches.Values.CopyTo(retArray, 0);
}
else
{
retArray = new Match[theMatches.Count];
theMatches.CopyTo(retArray, 0);
}

return (retArray);

}


static void Main(string[] args)
{

// Sample Text
string Text=@"Search Tutorials & Message Boards
Silverlight WPF WCF WWF LINQ
JavaScript AJAX ASP.NET XAML
C# VB.NET VB 6.0 GDI+ IIS XML
.NET Generics Anonymous Methods Delegate
Visual Studio .NET Expression Blend Virus
Windows Vista Windows XP Windows Update
Windows 2003 Server Windows 2008 Server
SQL Server Microsoft Excel Microsoft Word
SharePoint BizTalk Virtual Earth
.NET Compact Framework Web Service

Recent Articles & Code Samples
Scheduling SSIS packages as job

Appending data to an existing file in VB.NET

Writing to a file using FileStream in VB.NET

Copy, delete and move files in VB.NET

Error handling in SSIS

Error connecting to FTP server using SSIS FTP task

Embedding an image in a mail

Sending email in SSIS using script task

ASP.NET Cache dependency example

How Export Data from DataGrid to Excel

Data Compression in SQL SERVER 2008";

string matchPattern = "ASP.NET";



FindSubStringRegex oFind = new FindSubStringRegex();
Console.WriteLine("UNIQUE MATCHES");
Match[] x1 = oFind.FindSubstrings(Text, matchPattern, true);
foreach(Match m in x1)
{
Console.WriteLine(m.Value);
}

Console.WriteLine( );
Console.WriteLine("ALL MATCHES");
Match[] x2 = oFind.FindSubstrings(Text, matchPattern, false);
foreach(Match m in x2)
{
Console.WriteLine(m.Value);
}



}


}
}


Visit: http://www.eggheadcafe.com/tutorials/aspnet/b6277277-89d0-4b4d-9bac-2cd77e929f19/to-extract-uniqueall-sub.aspx

No comments:

Post a Comment

Locations of visitors to this page