IT Community - Software Programming, Web Development and Technical Support

How to split a string by using a string usually in the c# dotnet...

This is a discussion on How to split a string by using a string usually in the c# dotnet... within the C# Programming forums, part of the Software Development category; How to split a string by using a string.Usually in the c# DOT NET, we can only split a ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 07-25-2007, 02:25 AM
Archer Archer is offline
D-Web Programmer
 
Join Date: Jun 2007
Posts: 52
Archer is on a distinguished road
Question How to split a string by using a string usually in the c# dotnet...

How to split a string by using a string.Usually in the c# DOT NET, we can only split a string by using a single character?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 07-25-2007, 02:26 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 881
kingmaker is on a distinguished road
Send a message via MSN to kingmaker
Default Re: How to split a string by using a string usually in the c# dotnet...

Hi...Using the below method, one can split the string by a string value very easily.

private string[] SplitByString(string testString, string split)
{
int offset = 0;
int index = 0;
int[] offsets = new int[testString.Length + 1];

while (index < testString.Length)
{
int indexOf = testString.IndexOf(split, index);
if (indexOf != -1)
{
offsets[offset++] = indexOf;
index = (indexOf + split.Length);
}
else
{
index = testString.Length;
}
}


string[] final = new string[offset + 1];
if (offset == 0)
{
final[0] = testString;
}
else
{
offset--;
final[0] = testString.Substring(0, offsets[0]);
for (int i = 0; i < offset; i++)
{
final[i + 1] = testString.Substring(offsets[i] + split.Length, offsets[i + 1] - offsets[i] - split.Length);
}
final[offset + 1] = testString.Substring(offsets[offset] + split.Length);
}
return final;
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript to split the string and store it in a Array? poornima HTML, CSS and Javascript Coding Techniques 1 05-13-2008 02:28 AM
Convert String to Date: new Date(string) oxygen Flash Actionscript Programming 3 03-07-2008 08:24 PM
String Kirubhananth ASP and ASP.NET Programming 2 02-25-2008 08:37 PM
Compare a string with String Array. oxygen C# Programming 5 12-28-2007 10:18 PM
What is diffrenece between string.compare and string.compareordinal shaalini ASP and ASP.NET Programming 3 12-28-2007 09:46 PM


All times are GMT -7. The time now is 04:44 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0