Get part of a string

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?


Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
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

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

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="));
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

mousemat24Author Commented:
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;
Fernando SotoRetiredCommented:
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.
mousemat24Author Commented:

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
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.