Get part of a string

Posted on 2009-12-25
Last Modified: 2012-05-08
Hi there

Wonder if you can help me on this Christmas day ?

I have a string like

string url = "default.aspx?id=123331&unid=233"


string url = "default.aspx?unid=14423331&id=233&test=33"


string url = "default.aspx?unid=14331"


string url = "default.aspx?test=31&id=233&test=33&unid=1"

As you can see the unid can be anywhere in the url string

I need to get the value for the unid's

so for the 1st one its 233
so for the 2nd one its 14423331
so for the 3rd one its 14331
so for the 4th one its 1

Hope this makes sense?


Question by:mousemat24
    LVL 26

    Expert Comment

    this select will give you...
    DECLARE @string	varchar(300)
    SET @string = 'default.aspx?id=123331&unid=233'
    --@string = 'default.aspx?unid=14423331&id=233&test=33'
    --@string = 'default.aspx?unid=14331'
    --@string = 'default.aspx?test=31&id=233&test=33&unid=1'
    SELECT SUBSTRING(@string, CHARINDEX('unid=', @string, 1) + 5 , ISNULL(NULLIF(CHARINDEX('&', @string, CHARINDEX('unid=', @string, 1)), 0), LEN(@string)+1) - (CHARINDEX('unid=', @string, 1) + 5))

    Open in new window

    LVL 62

    Accepted Solution

    Hi mousemat24;

    Using regular expressions will capture the needed info.

    using System.Text.RegularExpressions;
    List<string> data = new List<string>();
    foreach (string str in data)
        string unid = Regex.Match(str, @"(?i)unid=(\d+)").Groups[1].Value;
        Console.WriteLine("unid value is " + unid);

    Open in new window

    LVL 15

    Expert Comment

    hi mousemat24,

    You can do that using indexOf and substring like

    string url = "default.aspx?test=31&id=233&test=33&unid=1";
            url = url.Substring(url.IndexOf("unid="));

    Author Comment

    Thanks FernandoSoto for your help, and

    both to tigin44 and rajeeshmca. But I have to give the points to FernandoSoto because

    1st, it was a C# question and 2nd'ly rajeeshmca, the problem with your code is it need to find a &, with my problem it can be a ? or a &.

    But thanks for all your help.

    FernandoSoto, would you mind please explaining what you are doing here? its amazing code!!

    Regex.Match(str, @"(?i)unid=(\d+)").Groups[1].Value;
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi mousemat24;
     This solution is using Regular Expressions to get the location of the needed field. This statement uses the static object in the Regular Expression library:
     Regex.Match(str, @"(?i)unid=(\d+)").Groups[1].Value;
     The Match method returns the first pattern found in the string. The first parameter, str, is the string that Regex will look in to see if the pattern is found in. The second parameter, @"(?i)unid=(\d+)", is the pattern to be match in the first parameter. The pattern has the following meaning:
     (?i)         Is the Regex meta characters that states to ignore case in the string.
     unid=    Are characters to be matched in the string exactly, no Regex meaning.
     (              Is the Regex meta character for start of capture group, what is to be returned.
     \d           Is the Regex meta characters to match a single digit.
     +             Is the Regex meta character to match 1 or more characters.
     )              Is the Regex meta character for end of capture group
     So the pattern simply states to ignore case when matching then look for the characters unid= and when found if followed by digits start capturing until the last digit has been captured and return those digits in capture group 1.
     I hope that helps.

    Author Comment


    Thats got to be one of the best code I've see for getting certain parts of a string!!

    Thanks for explaining it to me
    LVL 62

    Expert Comment

    by:Fernando Soto
    Not a problem, glad I was able to help.  ;=)

    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 article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    21 Experts available now in Live!

    Get 1:1 Help Now