Solved

C# - extracting string from this data...

Posted on 2013-06-25
5
258 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
Comment Utility
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
Comment Utility
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
Comment Utility
here:
            string name = content.Split(new string[] { "&", "=", "?" }, StringSplitOptions.RemoveEmptyEntries)[2];

Open in new window

0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

12 Experts available now in Live!

Get 1:1 Help Now