• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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"

or

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

or

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

or

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?

Thanks


0
mousemat24
Asked:
mousemat24
1 Solution
 
tigin44Commented:
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

0
 
Fernando SotoCommented:
Hi mousemat24;

Using regular expressions will capture the needed info.

Fernando
using System.Text.RegularExpressions;

List<string> data = new List<string>();
data.Add("default.aspx?id=123331&unid=233");
data.Add("default.aspx?unid=14423331&id=233&test=33");
data.Add("default.aspx?unid=14331");
data.Add("default.aspx?test=31&id=233&test=33&unid=1");

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

0
 
rajeeshmcaCommented:
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="));
        Response.Write(url.Substring(url.IndexOf('=')+1).Split('&')[0].ToString());
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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;
0
 
Fernando SotoCommented:
 
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.
 
 Fernando  
0
 
mousemat24Author Commented:
FernandoSoto

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
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now