?
Solved

C# - extracting string from this data...

Posted on 2013-06-25
5
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 600 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 600 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 800 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

764 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