473,503 Members | 6,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with string.split

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.

Jan 4 '07 #1
12 3175
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.

Jan 4 '07 #2
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.

Jan 4 '07 #3
Try

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

//...

s.Split(splitStrings, StringSplitOptions.None);

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

Jan 4 '07 #4
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.

Roman Wagner wrote:
Try

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

//...

s.Split(splitStrings, StringSplitOptions.None);

//...
But i think you should better learn how to use a database.
Jan 4 '07 #5
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.
--
Ignacio Machin
machin AT laceupsolutions com
Jan 4 '07 #6
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
Jan 4 '07 #7
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<stringinstead of an ArrayList.

Best Regards,
Dustin Campbell
Developer Express Inc.
Jan 4 '07 #8
ga********@myway.com wrote:
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
--
Tom Porterfield
Jan 4 '07 #9
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);
}

Jan 4 '07 #10
Please read "I then need to the array member S"

as

"I then need to add the array member S"

Thankyou very much.

Jan 4 '07 #11
Tom Porterfield wrote:
ga********@myway.com wrote:
>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);
--
Tom Porterfield
Jan 4 '07 #12
Thanks Tom, that's done what I needed!

Gary.

Jan 4 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
31156
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
27171
by: Blue Ocean | last post by:
In short, it's not working right for me. In long: The program is designed to read numbers from an accumulator and speak them out loud. Unfortunately, the class that contains the method to...
5
2712
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
3
2203
by: Jan | last post by:
Hi! I have to split a string with a pattern which contains sometimes chars like + \ (the code is needed in an interpreter, written in Perl, of my own scripting language, so I never know the...
5
1113
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
6
3776
by: JLW | last post by:
Here's my variables: string FileName string tmpSpl string FilePath Now, FileName has full path/filename, so, how do I Split FileName at the "\" char into tmpSpl, then redim preserve tmpSpl by...
1
3877
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
14
34878
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
4
1541
by: Michael | last post by:
after update dataTable with this codes, DataRow drNewRow = ff.m_dtWords.NewRow(); drNewRow=str; drNewRow=frequency; ff.m_dtWords.Rows.Add(drNewRow);...
0
2042
by: Iridium | last post by:
Greetings, I am trying to get a JPG Frame from a MJPG Stream. A MJPG is basically a stream of JPGs which are splitted by a special boundary string. So I tried to get the stream, split it by the...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.