Getting strings out of a big one with lots of \r\n

Greetings! :)

I recently started programming with C# yesterday - I must say it's a bliss to program in it, and even better with the Visual Studio C# Express IDE, great job! :D

^_^ Now onto my question ^^... I connect to a server which sends me a big string with carriage returns, it LOOKS like this:

1011 INFO "Welcome to the server"
1000 ERROR "You are not authorized"
0020 MESSAGE User100 "This is a message! :D"

However, as C# gets it (I think?) and the server sends it it's like...

1011 INFO "Welcome to the server"\r\n1000 ERROR "You are not authorized"\r\n0020 MESSAGE User100 "This is a message! :D"

I need to get the first 4 numbers and the description to show the data correctly to the end user. However in a format like that I can't. My question is, how would I get the data as shown in the first way, so I can "loop through each line and do my stuff with each one" for saying it that way?

I tried using:

char[] chtxt;
string msgid;
chtxt = txt.ToCharArray(); // txt is the data gotten from a NetworkStream.Read
msgid = chtxt[0].ToString() + chtxt[1].ToString() + chtxt[2].ToString();
Console.WriteLine(msgid);

But it's a bit orthodox and I know there must be another way. Using that I can get the first 3 characters of the whole string, but like that, as awhole and I need it split in lines for being able to identify each one :(

Thanks in advance :)

[1524 byte] By [DARKGuy] at [2007-12-25]
# 1

the \r\n are a carriage return characters, when you actually display them visually to the screen like a messagebox, textbox (multiline enabled) or a label, or even write to file, you won't see these.

So, what you could do is do a split on the \r\n, so it splits the message into an array:

string[] theMessages = theOriginalString.Split(new char[] {'\r', '\n'});

this would now have everything in the array, split at the \r\n characters. So this:

1011 INFO "Welcome to the server"\r\n1000 ERROR "You are not authorized"\r\n0020 MESSAGE User100 "This is a message! :D"

will become this:

1011 INFO "Welcome to the server"

1000 ERROR "You are not authorized"

0020 MESSAGE User100 "This is a message! :D"

Another way of just getting numbers would be to use Regex - regular expressions, pattern searching algorithms. These are expensive but good at doing the job but takes a while to get your head round it.

So if you went for the first suggestion, then to get the numbers only you have a couple of options.

First is to iterate through each character to see if its a number until no more numbers - can take a while and not great way of doing things

another way is that if you know the exact amount of characters, you can just read the number of characters:

string theNumber = theString.SubString(0, 4);

this will return back to you the contents of "theString" variable, starting from the first chracter, reading for 4 characters.

If you don't know how long the numbers will be, then I guess its better to use the option above the last one - going through each character until no more numbers found:

string theNumber = String.Empty;

foreach(Char curChar in theCurrentString)

{

if (Char.IsNumber(curChar))

{

theNumber += curChar.ToString();

}

else

{

break; //first instance of a non number, therefore we don't need to concatinate to our number string

}

}

does this help/give you some pointers?

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2
You're AWESOME!! that frickin' helped me LOTS! o.o it was exactly what I was searching for! HUGE thanks!!!

^_^ I might post later about Regular Expressions if I ever decide to use them in my program, but for now I think I have the base for converting my chat client to pure C#

(for some reason it didn't let me mark your post as answer and to say it was helpful, but I'll say it here ^_^).

DARKGuy at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...