problem with string.split

G

garyusenet

string[] lines = File.ReadAllLines(@"c:\text\history.txt");

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string delimit = ";";
string[] currentline = s.Split(";");

MessageBox.Show(s);

}

I am trying to incorporate some basic database functionality into my
programme using a text file and arrays. I am not using 'database'
technologies because i dont understand them, and have spent days trying
to so am going back to a basic text file.

I'm have read the contents of a text file into an array called lines,
line by line. This works fine.

The file is structured so that each line represents one record.
Each line record follows this type of pattern : -

date ; time ; name ; address ; some other info ; some more info ;
etc...

I am trying to write some code that will return an array of records
that match a given name.
My idea was take each line of the file in turn, and split it into a
'currentline' array, I could then query the 2nd element of the
currentline array and check to see if it matches the search name.

If it does then I can add the currentline to the results array.
If it doesn't I can ignore it and examine the next line.

However it's not working, there's something wrong with the way i'm
using Split, it seems to be looking for an array delimiter and not a
single string.

Any ideas how I can get it to work please?

Thanks,

Gary.
 
G

garyusenet

OK. I've managed to sort the string split now and have the following.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());
}

How do i query the second elemenet of the currentline array to check if
it matches a given value, and if it does, and the string S to the
results array, and if it doesn't continue with the foreach loop?

Thankyou,

Gary.
 
G

garyusenet

OK. I've managed to sort the string split now and have the following.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());
}

How do i query the second element of the currentline array to check if
it matches a given value, and if it does, and the string S to the
results array, and if it doesn't continue with the foreach loop?

Thankyou,

Gary.
 
R

Roman Wagner

Try

string[] splitStrings = new string[] {";"};

//...

s.Split(splitStrings, StringSplitOptions.None);

//...


But i think you should better learn how to use a database.
 
G

garyusenet

Thankyou Roman, I had just about figured the split part out, it's the
array checking and adding i'm stuck with atm!

Thank you very much,

Gary.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I am trying to incorporate some basic database functionality into my
programme using a text file and arrays. I am not using 'database'
technologies because i dont understand them, and have spent days trying
to so am going back to a basic text file.

IMO if you do not understand DB you should try again, they are the core of
this field


I am trying to write some code that will return an array of records
that match a given name.
My idea was take each line of the file in turn, and split it into a
'currentline' array, I could then query the 2nd element of the
currentline array and check to see if it matches the search name.

I do not see this in your provided code, where is it?
If it does then I can add the currentline to the results array.

I do not think you can do this, you should use an ArrayList as you do not
know beforehand the number of rows.

However it's not working, there's something wrong with the way i'm
using Split, it seems to be looking for an array delimiter and not a
single string.

Split does expect a char, not a string, you have to change the split from

string[] currentline = s.Split(";");

To:

string[] currentline = s.Split( new char[] { ';'} );


In anycase I advise you to buy a programming book, you seems to need it.
 
H

Hans Kesting

string[] lines = File.ReadAllLines(@"c:\text\history.txt");
foreach (string s in lines)
{
ArrayList results = new ArrayList();

string delimit = ";";
string[] currentline = s.Split(";");

MessageBox.Show(s);

}


try
string[] currentline = s.Split(';');

Note the single quotes around the ; to specify the *character* ';'
rather than a string containing a single ';' (subtle point, but
important in this case).

You could use a list of characters to split on every one of them. You
can't split on some substring.

Hans Kesting
 
D

Dustin Campbell

I do not think you can do this, you should use an ArrayList as you do
not know beforehand the number of rows.

Or, if you are using .NET 2.0 or 3.0, use a List<string> instead of an ArrayList.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
T

Tom Porterfield

OK. I've managed to sort the string split now and have the following.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());
}

How do i query the second elemenet of the currentline array to check if
it matches a given value, and if it does, and the string S to the
results array, and if it doesn't continue with the foreach loop?

if (currentline[1] == "some_preset_value")
// add S to results
 
G

garyusenet

Hi all there seems to be a delay with the newsgroup i'm using, I've
already sussed the string.split and need no further suggestions on how
to implement it please. i've already sorted the splitting of the string
out and have that under my hat.

I need to know how to query the third element of the currentline array,
and check if it matches a given string, e.g. "name". If it does match
this string I then need to the array member S to my results array/array
list.

Thankyou for any advice you may have on the matter,

Gary.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());


MessageBox.Show(s);


}
 
G

garyusenet

Please read "I then need to the array member S"

as

"I then need to add the array member S"

Thankyou very much.
 
T

Tom Porterfield

Tom said:
OK. I've managed to sort the string split now and have the following.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());
}

How do i query the second elemenet of the currentline array to check if
it matches a given value, and if it does, and the string S to the
results array, and if it doesn't continue with the foreach loop?

if (currentline[1] == "some_preset_value")
// add S to results

Syntax for that would be results.Add(s);
 

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