Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Trying to split an array on a tab

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AMP - 11 Mar 2008 17:32 GMT
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[i] = ArrayString.Split(stringSeparators,
StringSplitOptions.None);
                   i++;
               }

Thanks
Mike
Jon Skeet [C# MVP] - 11 Mar 2008 17:38 GMT
> 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[i] = 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
AMP - 11 Mar 2008 18:00 GMT
> > I am trying to split an Array into another array. Each value in the
> > array has tab delimited strings.
[quoted text clipped - 22 lines]
>
> 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
Jon Skeet [C# MVP] - 11 Mar 2008 19:22 GMT
> 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.)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 11 Mar 2008 19:33 GMT
> > Its one long string.
> > There are \r\n's at the end of rows and and \t's at the end of
[quoted text clipped - 25 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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
Jon Skeet [C# MVP] - 11 Mar 2008 19:49 GMT
<snip>

> 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
:)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.