Trying to split an array on a tab

A

AMP

Hello,
I am trying to split an Array into another array. Each value in the
array has tab delimited strings.
I am getting:
Cannot implicitly convert type 'string[]' to 'string'


I am trying this:
string[] lineSeparators = new String[]{"\r\n"};
string[] Channel =
FileName.Split(lineSeparators,StringSplitOptions.None); //This part
works
string[] Sample = new string[Channel.Length];
string[] stringSeparators = new String[] { "\t" };
foreach (string ArrayString in Channel)


{
int i = 0;
Sample = ArrayString.Split(stringSeparators,
StringSplitOptions.None);
i++;
}

Thanks
Mike
 
J

Jon Skeet [C# MVP]

I am trying to split an Array into another array. Each value in the
array has tab delimited strings.
I am getting:
Cannot implicitly convert type 'string[]' to 'string'

And it's right. You've got

string[] Sample

which means that every element of Sample is a string.

You're then trying:
Sample = ArrayString.Split(stringSeparators,
StringSplitOptions.None);

(and then reinitializing i next time round the loop, by the way)

string.Split returns an array of strings, not a single string - so you
quite possibly want

string[][] Sample = new string[Channel.Length][];

However, I suspect there's a rather better way of doing this. Could
you give an example of the input you've got, and the output you want?

Jon
 
A

AMP

I am trying to split an Array into another array. Each value in the
array has tab delimited strings.
I am getting:
Cannot implicitly convert type 'string[]' to 'string'

And it's right. You've got

string[] Sample

which means that every element of Sample is a string.

You're then trying:
Sample = ArrayString.Split(stringSeparators,
StringSplitOptions.None);

(and then reinitializing i next time round the loop, by the way)

string.Split returns an array of strings, not a single string - so you
quite possibly want

string[][] Sample = new string[Channel.Length][];

However, I suspect there's a rather better way of doing this. Could
you give an example of the input you've got, and the output you want?

Jon


Jon,
Its one long string.
There are \r\n's at the end of rows and and \t's at the end of
datapoints
I want each row to be an Array by itself.

3 1466 1462 1531 1465 1279 1490 1454 1441 1457 1453 1460 1459 1321
1457 1443 1443 55
75 1471 1469 1194 1467 2170 1491 1453 1442 1457 1455 1459 1460 2193
1459 1443 1443 127
148 1471 1472 1481 1463 1780 1490 1453 1442 1458 1455 1459 1459 1759
1455 1444 1442 200
220 1473 1470 1482 1465 843 1491 1453 1441 1458 1454 1459 1459 321
1455 1443 1443 272
293 1473 1480 1461 1465 1375 1486 1453 1443 1458 1455 1460 1459 1208
1457 1443 1442 344
365 1471 1472 1454 1463 1442 1490 1260 1446 1457 1456 1461 1460 1364
1457 1444 1441 417
438 1469 1459 1450 1460 1449 1489 2117 1444 1375 1455 1461 1459 1419
1458 1444 1442 489
510 1469 1463 1448 1460 1451 1491 1680 1443 2545 1463 1459 1460 1437
1459 1445 1442 562
582 1470 1466 1447 1459 1451 1489 1165 1435 1716 1454 1459 1460 1444
1458 1445 1443 634
655 1471 1478 1447 1461 1451 1489 1361 1440 483 1459 1459 1460 1447
1457 1444 1442 707
727 1469 1461 1447 1460 1452 1489 1420 1441 1004 1452 1458 1460 1448
1457 1443 1443 779
800 1468 1465 1447 1460 1453 1491 1441 1443 1313 1455 1460 1460 1448
1457 1444 1443 851
872 1462 1467 1447 1461 1454 1490 1449 1440 1407 1457 1461 1459 1448
1457 1443 1442 924
944 1462 1467 1446 1462 1454 1491 1451 1442 1439 1455 1460 1459 1449
1456 1443 1441 996

Thanks
mike
 
J

Jon Skeet [C# MVP]

AMP said:
Its one long string.
There are \r\n's at the end of rows and and \t's at the end of
datapoints
I want each row to be an Array by itself.

Do you particularly need the container to be an array? Are you happy
with a list instead?

Something like:

List<string[]> output = new List<string[]>();
foreach (string line in originalText.Split(new string[]{"\r\n"},
StringSplitOptions.None))
{
output.Add(line.Split('\t'));
}

Alternatively, if you're using C# 3 and .NET 3.5, and don't mind an
IEnumerable<string[]>:


var output = originalText.Split(new[]{"\r\n"}, StringSplitOptions.None)
.Select(line => line.Split('\t'));

(Call ToList if you'd rather get a list out.)
 
A

AMP

AMP said:
Its one long string.
There are \r\n's at the end of rows and and \t's at the end of
datapoints
I want each row to be an Array by itself.

Do you particularly need the container to be an array? Are you happy
with a list instead?

Something like:

List<string[]> output = new List<string[]>();
foreach (string line in originalText.Split(new string[]{"\r\n"},
                                           StringSplitOptions.None))
{
    output.Add(line.Split('\t'));

}

Alternatively, if you're using C# 3 and .NET 3.5, and don't mind an
IEnumerable<string[]>:

var output = originalText.Split(new[]{"\r\n"}, StringSplitOptions.None)
                         .Select(line => line.Split('\t'));

(Call ToList if you'd rather get a list out.)

Jon,
I am using your first suggestion,and it works. I need to do more
analysing of the numbers so this is best.
BUT, I am getting 19 elements for the Sub array and there are only 18
datapoints. There is an extra empty string element at the end of each.
Why? and can I get rid of it without for/eaching every Array?
Thanks
Mike
 
J

Jon Skeet [C# MVP]

I am using your first suggestion,and it works. I need to do more
analysing of the numbers so this is best.
BUT, I am getting 19 elements for the Sub array and there are only 18
datapoints. There is an extra empty string element at the end of each.
Why? and can I get rid of it without for/eaching every Array?

The easiest thing would be to use StringSplitOptions.RemoveEmptyEntries
:)
 

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

Top