0

I am having an issue trying to receive a string that I need to split as one part of the string has a name and the other part of the string has a location. although I am unsure how to split this data, as when it is received, it should come back as:

"POST /JACK\r\n\r\nis at this building\r\n"

name part is "POST /JACK\r\n\r\n".

location part is "is at this building\r\n".

The code I use to read this incoming message is:

line = sr.ReadLine();

The message is sent through a connected socket to me by an address and port. But when I receive this message, line is returned as: "POST /JACKis at this building"

I did try and use this code before:

            var str = line;
            var Sorted = str.Replace("\\r\\n", System.Environment.NewLine);
            string[] splitted = Sorted.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Which basically is meant to look for lines in the string so it can split and sort it but from the looks of it, I cannot use it if I am not getting the "\r\n" from the message, which means I cannot split it.

How would I split the information from the message provided that the message is delivered as: "POST /JACKis at this building" delivered to me?

8
  • OK so what is the rule for where the break should happen Commented Mar 6, 2022 at 23:47
  • the break? I'm unsure what you mean, if you are on about the message, I cannot do a ReadToEnd(); as that causes an error (the message does not tell when to stop). So I have to use a ReadLine();. I am trying to basically get that message into args. so if I had for example: string[] line = null; line = sr.readline(); And I had that incoming message, I am trying to basically make it so I can split the message up by each new line. But I am having trouble over it. I'm sure it's simpler than it is but I am confused. Commented Mar 6, 2022 at 23:54
  • you ask 'how can I chop this up?' "POST /JACKis at this building" My question is - what is the chopping rule. Do you want "P","OST", "/JACKis", " at this building". Or maybe "POST /", "JACK is at the building", or what Commented Mar 6, 2022 at 23:57
  • I want it to be that the variable Name contains "jack" and the variable Location contains "at this building". I know that I can use things like .remove(0,6) or .replace("POST /", ""); which I have used. The message is like that because it comes formatted in a 0.9 protocol Commented Mar 7, 2022 at 0:10
  • so I guess I want it to be "JACK" "is at the building". Commented Mar 7, 2022 at 0:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.