-1

https://www.dgdgfdgdfg.com/profile/view?id=246925795&authType=name&authToken=sygl&trk=prof-sb-browse_map-name here i need to split only 246925795 from these where string between ?id and &authType please someone help me

Ravindran
  • 37
  • 10
  • These are query string in the url. Can you show some code, what you have tried? – Hassan Jun 20 '14 at 11:10
  • actually i am inserting multiple links using textbox while inserting i need to check using id, and these is linked in url , here only id is common, before i checked with full url i have two or more urls of the same – Ravindran Jun 20 '14 at 11:13
  • You have this url in a string variable? – Hassan Jun 20 '14 at 11:14
  • else if (!string.IsNullOrEmpty(txtmultilines.Text)) { lblcorrect.Text = ""; lblerror.Text = ""; string txt = txtmultilines.Text; string[] lst = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); foreach (string element in lst) { // Label2.Text = ""; Label2.Text += element; – Ravindran Jun 20 '14 at 11:15
  • @Ravidran checkout this [Fiddle](https://dotnetfiddle.net/Y0FPc4). A very simple approach is to create a new Uri and ParseQueryString. – Hassan Jun 20 '14 at 11:30

2 Answers2

1

Try this :

int start = str.IndexOf("id=") + "id=".Length;
int end = str.IndexOf("&authType");
var result = str.Substring(start, end-start);

Better approach is to change the URL into object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

string s = @"https://www.dgdgfdgdfg.com/profile/view?id=246925795&authType=name&authToken=sygl&trk=prof-sb-browse_map-name";
Uri myUri = new Uri(s);
string sId = HttpUtility.ParseQueryString(myUri.Query).Get("id"); //246925795
Hassan
  • 5,360
  • 2
  • 22
  • 35
Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
  • Only works when no other parameter ending in `id` exists before `id=` and when `id=` is actually present, when `id={id}` is directly followed by `&authType`. Not maintainable, not robust, too specific answer to benefit anyone other than OP, and worst of all: reinventing the wheel. .NET has a perfect URL parser, see [duplicate](http://stackoverflow.com/questions/659887/get-url-parameters-from-a-string-in-net). -1. – CodeCaster Jun 20 '14 at 11:30
-1

Try this:

string url = "https://www.dgdgfdgdfg.com/profile/view?id=246925795&authType=name&authToken=sygl&trk=prof-sb-browse_map-name";
            int a=url.IndexOf('=');
            int b=url.IndexOf('&');
            if(a!=-1 && b!=-1)
            {
                int id = Int32.Parse(url.Substring(a + 1, (b - a) - 1));
            }
malkam
  • 2,337
  • 1
  • 14
  • 17