A more useful Split?

  • Thread starter Thread starter mb
  • Start date Start date
M

mb

I was wondering if there is an easy, more useful Split function that will
split with a string delimiter like "<>" or "////"?
 
mb said:
I was wondering if there is an easy, more useful Split function that will
split with a string delimiter like "<>" or "////"?

using System.Text.RegularExpressions;

string strTest = "One<>Two<>Three<>Four<>Five";
string[] astrTest = Regex.Split(strTest, "<>");
 
THANKS!!

By the way, what does Regex stant for. I keep calling it "Rejects" :-)

Mark Rae said:
mb said:
I was wondering if there is an easy, more useful Split function that will
split with a string delimiter like "<>" or "////"?

using System.Text.RegularExpressions;

string strTest = "One<>Two<>Three<>Four<>Five";
string[] astrTest = Regex.Split(strTest, "<>");
 
mb said:
THANKS!!

By the way, what does Regex stant for. I keep calling it "Rejects" :-)

REGular EXpressions
Mark Rae said:
mb said:
I was wondering if there is an easy, more useful Split function that
will
split with a string delimiter like "<>" or "////"?

using System.Text.RegularExpressions;

string strTest = "One<>Two<>Three<>Four<>Five";
string[] astrTest = Regex.Split(strTest, "<>");
 
Is this faster than the string.Split method?


Daniel O'Connell said:
mb said:
THANKS!!

By the way, what does Regex stant for. I keep calling it "Rejects" :-)

REGular EXpressions
Mark Rae said:
I was wondering if there is an easy, more useful Split function that
will
split with a string delimiter like "<>" or "////"?

using System.Text.RegularExpressions;

string strTest = "One<>Two<>Three<>Four<>Five";
string[] astrTest = Regex.Split(strTest, "<>");
 
You could implement your own splitting function.
Here's one I have done on a hurry that is ugly but working:

public static string[] StringSplit(string input, string delimiter)
{
ArrayList arr = new ArrayList();

int pos = -1;
int acheived = 0;
bool founddelimiter = false;
StringBuilder splitted = new StringBuilder();
StringBuilder temp = new StringBuilder();

while ( pos++ < input.Length -1 )
{
if ( input[pos] != delimiter[acheived] )
{
founddelimiter = false;
splitted.Append(temp.ToString());
splitted.Append( input[pos] );
acheived = 0;
temp.Length = 0;
}
else
{
if ( acheived == delimiter.Length - 1 )
{
temp.Length = 0;
arr.Add( splitted.ToString() );
founddelimiter = true;
splitted.Length = 0;
acheived = 0;
}
else
{
temp.Append(input[pos]);
acheived++;
}
}
}

if ( splitted.Length > 0 )
arr.Add( splitted.ToString() );

if ( founddelimiter )
arr.Add("");

return (string[]) arr.ToArray(typeof(string));

}
 
Is this faster than the string.Split method?

I doubt it, but you don't have a lot of choice in this instance because the
string.Split method won't accept multiple characters to split on. Your only
other option is to write your own splitting function...
 
THANKS!!

By the way, what does Regex stant for. I keep calling it "Rejects" :-)

The clue is in the namespace...

using System.Text.RegularExpressions;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top