473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Splitting up a string

I am new to c# and I am currently trying to make a program to retrieve
Battlefield 2 game stats from the gamespy servers. I have got it so I
can retrieve the data but I do not know how to cut up the data to
assign each value to its own variable. So right now I am just saving
the data to a txt file and when I look in the text file all the data is
there.

Not sure if this matters but when I open the text file in Word pad
(Rich Text) It shows something like this.

ŽO
H D
asof 1133092198
H D
pid 43974309
nick Opettaja
scor 1459
jond 1118881320
wins 28
loss 47
.....
.....

But when I open it in notepad (Raw Text) Everything is on one line with
squares indicating linebreaks.

So if anyone has any suggestions on how I can break up each line into
its own variable that would be greatly appreciated. And sorry If im a
little vague on information, I am new to c# and still learning, If you
need more information just let me know.

Thanks.

Nov 27 '05 #1
20 3712
"Opettaja" <op******@gmail .com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
<snip>
So if anyone has any suggestions on how I can break up each line into
its own variable that would be greatly appreciated. And sorry If im a
little vague on information, I am new to c# and still learning, If you
need more information just let me know.


This might be helpful:

http://msdn.microsoft.com/library/de...xtFromFile.asp

It reads the file a line at a time. The problem with Notepad is probably
down to the difference in linefeeds/carraige returns on *nix/Windows. It
might be that the file includes \r or \n rather than both. There's probably
a way to configure what the above code uses as as a line end.

HTH
Nov 27 '05 #2
Simply determine, with a hex editor if need be, what the field delimiter
and end of line characters are. I suspect you'll find that they are
tabs and line feeds, respectively.

Once you determine what delimits each field then you can use the
String.Split() method to break the fields up into an array. Read one
line, break that line into fields, process as desired, and repeat until
done.

--Bob

Opettaja wrote:
I am new to c# and I am currently trying to make a program to retrieve
Battlefield 2 game stats from the gamespy servers. I have got it so I
can retrieve the data but I do not know how to cut up the data to
assign each value to its own variable. So right now I am just saving
the data to a txt file and when I look in the text file all the data is
there.

Not sure if this matters but when I open the text file in Word pad
(Rich Text) It shows something like this.

ŽO
H D
asof 1133092198
H D
pid 43974309
nick Opettaja
scor 1459
jond 1118881320
wins 28
loss 47
....
....

But when I open it in notepad (Raw Text) Everything is on one line with
squares indicating linebreaks.

So if anyone has any suggestions on how I can break up each line into
its own variable that would be greatly appreciated. And sorry If im a
little vague on information, I am new to c# and still learning, If you
need more information just let me know.

Thanks.

Nov 27 '05 #3
Okay I got the lines split up into individual lines, but now how do I
split up the individual lines into 2 variables using Split and spliting
by just spaces. Here is what I have right now

string[] lines = reader.ReadStri ng().Split(new Char[] {'\r','\n'});

for (int i = 0; i < lines.Length; i++)
{
string[] output = lines[i].Split(new Char[]{' '});
listBox1.Items. Add(output[1]);
}

What do I put in the Char{' '} to make it split by the spaces?

Nov 27 '05 #4
If spaces are in fact the delimiter then what you have is fine. You
want a character array of characters to split on, and {' '} creates an
array of one character, the space.

Be aware that if there are multiple spaces on a line then you'll get
more than two fields. For example:

Foo Bar
Fu Bar

On the Foo line there are two spaces between Foo and Bar. On the 2nd
line there are three spaces. Splitting on a space will give you three
strings from the first line ("Foo", "", and "Bar") and four strings from
the second line ("Fu","","" and "Bar").

You can either account for this in your parsing of the array or
normalize the whole line before splitting, by removing adjacent spaces,
along the lines of:

while (theLine.IndexO f(" ") > -1) {
theLine = theLine.Replace (" "," ");
}

--Bob

Opettaja wrote:
Okay I got the lines split up into individual lines, but now how do I
split up the individual lines into 2 variables using Split and spliting
by just spaces. Here is what I have right now

string[] lines = reader.ReadStri ng().Split(new Char[] {'\r','\n'});

for (int i = 0; i < lines.Length; i++)
{
string[] output = lines[i].Split(new Char[]{' '});
listBox1.Items. Add(output[1]);
}

What do I put in the Char{' '} to make it split by the spaces?

Nov 28 '05 #5
if anyone wants to use regular expressions, i can offer up a solution,
but every time i post a solution using regular expressions, i get beaten
up for it. when i say 'beaten up' i mean to say that the replies are
often similar to 'regular expressions are hard to read, avoid them' and
'that's bad style'.

well i'm friggin' sorry but i grew up on Perl and regular expressions
are second nature to me.

yes i'm bitter.

*goes away*

Bob Grommes wrote:
If spaces are in fact the delimiter then what you have is fine. You
want a character array of characters to split on, and {' '} creates an
array of one character, the space.

Be aware that if there are multiple spaces on a line then you'll get
more than two fields. For example:

Foo Bar
Fu Bar

On the Foo line there are two spaces between Foo and Bar. On the 2nd
line there are three spaces. Splitting on a space will give you three
strings from the first line ("Foo", "", and "Bar") and four strings from
the second line ("Fu","","" and "Bar").

You can either account for this in your parsing of the array or
normalize the whole line before splitting, by removing adjacent spaces,
along the lines of:

while (theLine.IndexO f(" ") > -1) {
theLine = theLine.Replace (" "," ");
}

--Bob

Opettaja wrote:
Okay I got the lines split up into individual lines, but now how do I
split up the individual lines into 2 variables using Split and spliting
by just spaces. Here is what I have right now

string[] lines = reader.ReadStri ng().Split(new Char[] {'\r','\n'});

for (int i = 0; i < lines.Length; i++)
{
string[] output = lines[i].Split(new Char[]{' '});
listBox1.Items. Add(output[1]);
}

What do I put in the Char{' '} to make it split by the spaces?

Nov 28 '05 #6
lol .. sorry for the "off topic" reply but, just wanted to let you know
that at 3:27 am in the middle of a tedious debugging session, you gave
me a giggle.

BTW, I totally agree. Regular expressions are like everything if you
haven't seen them before. They're not so bad once you get used to them.
They can be overkill in some situations, but in others they are by far
the best way.

Oh and if anyone is going to beat up JJ and thinks I'm guilty by
association, I wasn't here.

*tip toes away*
jeremiah johnson wrote:
if anyone wants to use regular expressions, i can offer up a solution,
but every time i post a solution using regular expressions, i get beaten
up for it. when i say 'beaten up' i mean to say that the replies are
often similar to 'regular expressions are hard to read, avoid them' and
'that's bad style'.

well i'm friggin' sorry but i grew up on Perl and regular expressions
are second nature to me.

yes i'm bitter.

*goes away*

Bob Grommes wrote:
If spaces are in fact the delimiter then what you have is fine. You
want a character array of characters to split on, and {' '} creates an
array of one character, the space.

Be aware that if there are multiple spaces on a line then you'll get
more than two fields. For example:

Foo Bar
Fu Bar

On the Foo line there are two spaces between Foo and Bar. On the 2nd
line there are three spaces. Splitting on a space will give you three
strings from the first line ("Foo", "", and "Bar") and four strings
from the second line ("Fu","","" and "Bar").

You can either account for this in your parsing of the array or
normalize the whole line before splitting, by removing adjacent
spaces, along the lines of:

while (theLine.IndexO f(" ") > -1) {
theLine = theLine.Replace (" "," ");
}

--Bob

Opettaja wrote:
Okay I got the lines split up into individual lines, but now how do I
split up the individual lines into 2 variables using Split and spliting
by just spaces. Here is what I have right now

string[] lines = reader.ReadStri ng().Split(new Char[] {'\r','\n'});

for (int i = 0; i < lines.Length; i++)
{
string[] output = lines[i].Split(new Char[]{' '});
listBox1.Items. Add(output[1]);
}

What do I put in the Char{' '} to make it split by the spaces?

Nov 28 '05 #7
Simon wrote:
lol .. sorry for the "off topic" reply but, just wanted to let you know
that at 3:27 am in the middle of a tedious debugging session, you gave
me a giggle.

BTW, I totally agree. Regular expressions are like everything if you
haven't seen them before. They're not so bad once you get used to them.
They can be overkill in some situations, but in others they are by far
the best way.


The trouble is that they're overkill in *many, many* situations - but
some people use them as the "default" way of performing string
manipulation.

If you want to operate on pattern, they're great. If you just want to
perform a simple replacement, or matching of an exact string, then
they're not only overkill, but they're *risky* overkill. You have to
remember all the characters which need escaping, etc. Furthermore, you
have to hope that maintenance engineers will remember those characters
too.

Want to find out whether one string starts with another? Use
String.StartsWi th.
Want to replace one (exact) string with another within a third? Using
String.Replace.
Want to match or replace patterns? Use Regex.

A lot of things are "not so bad once you get used to them" - but that
doesn't mean they're the best solution to the problem. I'm sure if you
work at it hard enough, you can become quite proficient at banging in
nails with a screwdriver - that doesn't mean it's a better tool than a
hammer for that particular task though.

Jon

Nov 28 '05 #8
Simon wrote:
lol .. sorry for the "off topic" reply but, just wanted to let you know
that at 3:27 am in the middle of a tedious debugging session, you gave
me a giggle.

BTW, I totally agree. Regular expressions are like everything if you
haven't seen them before. They're not so bad once you get used to them.
They can be overkill in some situations, but in others they are by far
the best way.

Oh and if anyone is going to beat up JJ and thinks I'm guilty by
association, I wasn't here.


As an addendum to my previous post - I was one of the people who "beat
up" JJ last time he suggested using a regular expression. It's an
example which demonstrates my point very well. Here's the code he
suggested:

if you're able to loop through the records in the very large files,
skip
processing when you see that the record you're on starts with "***".

using System.Text.Reg ularExpressions ;
....

foreach (string s in records) { // example code
Match m = Regex.Match(s, "^***");
if (m.Success) { // this is a header or footer
[...]

Now, JJ, who claims that regular expressions are "second nature" to him
got the regular expression wrong. The correct expression, as specified
by Greg Bacon, is @"^\*\*\*". Compare the solution which uses regular
expressions with my suggested solution:

if (s.StartsWith(" ***"))

Which one is easier to write? Well, JJ got the regex version wrong on
his first attempt. I somehow doubt that my version fails.

Which one is easier to read? Well, if you read my version out loud, it
says *exactly* what it's doing. The same can certainly *not* be said
for the regular expression version.

Is it any wonder that I think JJ's being a little over-zealous with his
use of regular expressions?

Jon

Nov 28 '05 #9
jeremiah johnson wrote:
if anyone wants to use regular expressions, i can offer up a solution,
but every time i post a solution using regular expressions, i get beaten
up for it. when i say 'beaten up' i mean to say that the replies are
often similar to 'regular expressions are hard to read, avoid them' and
'that's bad style'.


It's bad style to use them when they're unnecessary and are a more
complex solution to the problem than another one. If you look at the
recent thread which talked about finding lines which start with "***",
you'll see a much simpler solution than your suggested regular
expression - and one which, somewhat importantly, *works*.

In this case, however, I believe they're absolutely the right tool for
the job, because an actual pattern is involved. Personally I'd probably
read the file line by line, and then split each line with a regular
expression. The regular expression involved would be pretty simple at
that stage.

Jon

Nov 28 '05 #10

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

Similar topics

6
1926
by: qwweeeit | last post by:
Splitting with RE has (for me!) misterious behaviour! I want to get the words from this string: s= 'This+(that)= a.string!!!' in a list like that considering "a.string" as a word. Python 2.3.4 (#2, Aug 19 2004, 15:49:40) on linux2
5
2970
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
4
2019
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
2
2522
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between fields in a table...well, someone has been entering description fields with comas (,) in the description and now it is splitting between one field...example: "santa clause mushrooms, pens, cups and dolls" I somehow need to NOT split anything...
2
1490
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow: Dim s As String, h As String Dim delim(1) As Char delim(0) = "/"
6
303
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with shorter strings does not seem to work with this one. Ideas? Thanks, jvh
2
3269
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
4
2824
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to integers later. I've bene looking around, and everything I see seems more complicated than it should be! Help!
37
1851
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
8823
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
9530
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
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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
4593
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...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
3
2206
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.