Solved

C# - extracting string from this data...

Posted on 2013-06-25
5
261 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

Suggested Solutions

Title # Comments Views Activity
C# application error "Parameter Is Not Valid". 3 75
Data is not showing from images 15 36
Filename to be appended with DateTimeof Download 3 40
Error on link 14 36
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now