Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

Here is my url:
http://localhost:4127/Project/articles/test-article-2766.aspx[^]

from this url how can i get the number 2766

please any one can help me.

Thanks in advance.

please see the output. i try every solution but i can't get this.
i give the output for every solution.
this code is worked but every time display same number,it does not change along with url
C#
string pageUrl = HttpContext.Current.Request.Url.AbsoluteUri;
if(!string.IsNullOrEmpty(pageUrl))
{
string[] arr = pageUrl.Split('-');
string ur_number = (arr[arr.Length -1]).replace(".aspx"," ");
}


I think this problem is occured because i am using url rewriter for that it change the url but those codes
only get default url from config file. Plaese any one help me

i have write in config file
XML
<rewriter>
    <rewrite url="~/articles/(.+)-(.+).aspx" to="../articles/details.aspx?test-for-stream-5-2778"/>

  </rewriter>

plesae check this i think i am going something wrong in this url rewrite. i give this default url test-for-stream-5-2778 so every time request.url get this url only.
Posted
Updated 31-May-11 0:46am
v6

You need to use regular expression:

using System.Text.RegularExpressions;

Regex reg;
Match m;
string line = " http://localhost:4127/Project/articles/test-article-2766.aspx";
reg = new Regex(@"http://.*?/.*?/.*?/.*?(\d{4})");
m = reg.Match(line);
string str = m.Groups[1].ToString;



<edit>Error on Regex declaration, added the magical @ before the string
 
Share this answer
 
v2
Comments
JustWorking 31-May-11 4:27am    
In C# index start at 0 so replace m.Groups[0].ToString;
Remmeber to mark Accept Solution and 5 if it works for you:)
Sergey Alexandrovich Kryukov 31-May-11 4:45am    
Most adequate use of Regex, a 5.
--SA
Kim Togo 31-May-11 5:52am    
My 5 for the regex. But you can make it more simple.
Regex: (\d{4})\..*$
rahul dev123 31-May-11 6:06am    
regex shown error like unrecognized escape sequence
Hi,

Here's the code for your assignment.

C#
var url = @"http://localhost:4127/Project/articles/test-article-2766.aspx";
var split = url.Split('.');

if (split.Length > 0 && split[0].LastIndexOf('-') > -1) {
    var articleNo = split[0].Substring(split[0].LastIndexOf('-') + 1);
    Console.WriteLine(articleNo);
}


Good luck!
 
Share this answer
 
v2
Comments
rahul dev123 31-May-11 5:58am    
Your code shown Error like this:
System.ArgumentOutOfRangeException: StartIndex cannot be less than zero
Pong D. Panda 31-May-11 6:39am    
I updated my solution for further checking, take note that this code follows your format, having the article number in between the . of the aspx and the last '-' of your url.
Pong D. Panda 31-May-11 6:27am    
Tell me, what's the url when that error was thrown?
string pageUrl = HttpContext.Current.Request.Url.AbsoluteUri;
if(!string.IsNullOrEmpty(pageUrl))
{
   string[] arr = pageUrl.Split('-');
   string ur_number = (arr[arr.Length -1]).replace(".aspx"," ");
}
 
Share this answer
 
v3
Comments
rahul dev123 31-May-11 5:51am    
Your solution is work for get the value but one problem is every time it display the same value. it does not change along with url
Assuming your number length is only 4.
string URI = myURI.toString();
string result = URI.SubString(URI.Length-9,4)


The logic behind this 9 is simple (4 characters for aspx, 1 for the . and 4 for the number itself).
 
Share this answer
 
Comments
rahul dev123 31-May-11 6:01am    
SubString not supported
use this

C#
string [] a=System.IO.Path.GetFileName(Request.Path).Split('-');
       string [] b=a[2].Split('.');
       string val = b[0].ToString();
 
Share this answer
 
Comments
rahul dev123 31-May-11 6:02am    
your code shown error like this:
Index was outside the bounds of the array.
Use this code:

var url = @"http://localhost:4127/Project/articles/test-article-2766.aspx";
var split = url.Split('.');

if (split.Length > 0) {
   var articleNo = split[0].Substring(split[0].LastIndexOf('-'));
}
 
Share this answer
 
Comments
rahul dev123 31-May-11 5:58am    
Your code shown Error like this:
System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
string x = "http://localhost:4127/Project/articles/test-article-2766.aspx";
x = x.Replace("http://localhost:4127/Project/articles/test-article-2766.aspx", "2766");

lol
 
Share this answer
 
v2
C#
Try below Code. This is generic and should wotk for any number in your url.

string url = "http://localhost:4127/Project/articles/test-article-2766.aspx";

       int dashPosition = url.LastIndexOf("-");
       int dotPosition = url.LastIndexOf(".");

       int totalCharactersOfNumber = dotPosition - dashPosition -1;


       string output = url.Substring(url.LastIndexOf("-")+1, totalCharactersOfNumber);
 
Share this answer
 
Comments
rahul dev123 31-May-11 5:55am    
Your code error like this:
System.ArgumentOutOfRangeException: Length cannot be less than zero.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900