MrCatacroquer
MrCatacroquer

Reputation: 2068

Split string by a complete string separator

I need to split a string, for example AAA@AAA_@#@BBBBBB@#@CCCCCC, using as separator the complete string "_@#@_". The result i'm looking for is:

[0] AAA@AAA

[1]

[2] BBBBBB

[2]

[3] CCCCCC

I'm doing the following:

char[] sep = FIELD_SEPARATOR.ToCharArray();
ArrayList result = new ArrayList();
string[] fields = line.Split(sep);

Where FIELD_SEPARATOR is the string "_@#@" The thing is that i'm getting 2 records for the first field, and the "@" char is deleted from them.

[0] AAA

[1] AAA

...

Is there a way to do it? I'm using .NET Framework 1.1

Thanks in advance!

Upvotes: 1

Views: 1882

Answers (4)

Ankush Roy
Ankush Roy

Reputation: 1641

if,

string oldstring="AAA@AAA_@#@BBBBBB@#@CCCCCC";

then,

string[] parts = System.Text.RegularExpressions.Regex.Split(oldstring,"@#@");

This will give ,

parts[0]=AAA@AAA_

parts[1]=BBBBBB

parts[2]=CCCCCC

Will that Suffice...........

Upvotes: 4

Andrei
Andrei

Reputation: 4328

This should also work for you:

string[] bits = Regex.Split("AA@AAA_@#@BBBBBB@#@CCCCCC", "@#@");

Upvotes: 6

alexl
alexl

Reputation: 6851

To be more correct

line.Split(new string[] { "@#@" }, StringSplitOptions.None)

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

Does this not work?

string[] fields = line.Split(new string[] {"@#@"}, StringSplitOptions.None);

Upvotes: 6

Related Questions