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 (permalink)  
Old 07-25-2007, 03: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
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 03:26 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo 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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert String to Date: new Date(string) oxygen Flash Actionscript Programming 3 03-07-2008 09:24 PM
String Kirubhananth ASP and ASP.NET Programming 2 02-25-2008 09:37 PM
Compare a string with String Array. oxygen C# Programming 5 12-28-2007 11:18 PM
What is diffrenece between string.compare and string.compareordinal shaalini ASP and ASP.NET Programming 3 12-28-2007 10:46 PM
Encoding String Anandavinayagam PHP Programming 0 03-29-2007 12:49 AM


All times are GMT -7. The time now is 10:26 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0