473,787 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with string.split

string[] lines = File.ReadAllLin es(@"c:\text\hi story.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 3200
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(";".ToC harArray());
}

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(";".ToC harArray());
}

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(splitSt rings, StringSplitOpti ons.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(splitSt rings, StringSplitOpti ons.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.ReadAllLin es(@"c:\text\hi story.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<stringinst ead of an ArrayList.

Best Regards,
Dustin Campbell
Developer Express Inc.
Jan 4 '07 #8
ga********@mywa y.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(";".ToC harArray());
}

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_va lue")
// 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(";".ToC harArray());
MessageBox.Show (s);
}

Jan 4 '07 #10

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

Similar topics

5
31181
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 will split myString up using the delimiter of 1 space so that
5
27203
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 read off large numbers is only for integers. My intention is to split a String across the Regex of ".". However, this code does not work: private void doRealValueOf(String text) {
5
2726
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 last few bytes of uudecoded data are always mangled. Take a look of this hexdump output: Originals (decoded with Pan, each line is from a different file): 000c2c0 e1bf 00ff 2541 a9e4 a724 d9ff 0011a10 ff54 00d9 00093e0 fb4f a80d ffd9 c200 ffef...
3
2217
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 pattern exactly). split(/$pattern/, $string); It works well most of all time, but if the pattern is + the execution of the program stops and perl prints an error message.
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 until the CDO.DropDirectory.GetMessages() method is called. At this point, the files are locked until I shut down the service. After processing and delivery, I need to be able to delete all the files in the drop directory. I can delete them via...
6
3789
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 one less, the rejoin it all into FilePath. I can do this very easily in VB.NET, but can't find anything on how to do it in C#
1
3895
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 = New DirectoryEntry("LDAP://domain") Dim mySearcher As New DirectorySearcher(enTry) Dim resEnt As SearchResult mySearcher.Filter = ("(objectClass=*)") mySearcher.SearchScope = SearchScope.Subtree
14
34977
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, bytesRead) .... Dim parts() As String = data.Split(vbCrLf)
4
1551
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); ff.m_daDataAdapter.Update(ff.m_dtWords);
0
2061
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 boundary strings and save the JPG binary data into a file. The problem is, I cant save the data into a proper JPG file which I then open. This is my code: using System; using System.Collections.Generic; using System.Text; using System.IO; using...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10169
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
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 we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.