Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# - extracting string from this data...

Posted on 2013-06-25
5
Medium Priority
?
277 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:Meir Rivkin
Meir Rivkin 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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