Solved

C# - extracting string from this data...

Posted on 2013-06-25
5
267 Views
Last Modified: 2013-07-01
Hi,

I am writing a .net c# application.

I have the following string  called "content" which contains the following -

GET /cgi-bin/update.sh?name=Name1&ip= HTTP/1.1
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
Host: 192.168.1.118
Connection: Keep-Alive

Open in new window


What is the best way to extract "Name1" from the first line to another variable?

Thanks for any input
0
Comment
Question by:mhdi
5 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 150 total points
ID: 39277094
Something like (might be out by 1 but you can adjust):

string Result = Content.Substring(Content.IndexOf("name=")+5);
Result = Result.Substring(0, Result.IndexOf("&"));

Open in new window

0
 

Author Comment

by:mhdi
ID: 39277107
What does the +5 do?

The ID "Name1" will change and could be anywhere between 3 and 15 characters.

For that reason, I suspect this wont work for all instances?
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 150 total points
ID: 39277130
here:
            string name = content.Split(new string[] { "&", "=", "?" }, StringSplitOptions.RemoveEmptyEntries)[2];

Open in new window

0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39277136
If you check out the reference for what IndexOf does you'll see it returns the index of the first character in the string being searched for. However you want the string that follows "name=" so you need to add 5 characters on to move from the start of "name=" to the end.

The code snippet I have written should definitely work, its looking for the first character following "name=" (which shouldn't change), through to the first "&", and therefore certainly handles any number of chars in-between.

I think you've confused yourself in your example by using name and name one as the variable name and value :)
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 39277655
The two suggestions above are plausible, but both make assumptions about the existence and position of the "name" parameter. If you are assured that "name" will always be the first parameter, and that there will always be at least two parameters, then they should work fine. However, if you are not assured of these conditions, then you are going to encounter issues. The following seems to account for these conditions:

string result = System.Text.RegularExpressions.Regex.Match(content, "(?<=name=)[^& ]+").Value;

Open in new window


...where "result" will contain an empty string if "name" does not exist within the target string.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question