2014-11-21 20 views
0

我如何提取鏈接的C#最後5位數字,例如如何最後5位數字從一個鏈接

www.mywebsite.com?agent_id=12345 

現在我要到12345保存在session變量中提取

Session["agent_id"] = ??? 
+0

使用string.Substring(string.length減 - 5,string.length減),其

string agentId = Request.QueryString["agent_id"]; 

更多信息 – GANI 2014-11-21 00:40:51

+7

你爲什麼這樣做而不是閱讀'Request.QueryString'? – Claies 2014-11-21 00:41:11

+0

對安德魯來說,如果URL是'www.mywebsite.com?agent_id = 12345&hello = adf',該怎麼辦? – 2014-11-21 00:42:34

回答

2

如果您試圖從當前請求中獲取agent_id值,那麼Marcos的答案會爲您提供您想要的。然而,如果您所指的「鏈接」在某些其他字符串變量中,那麼最好的辦法就是使用正則表達式。

string someUrl = "www.mywebsite.com?agent_id=12345"; 
var match = Regex.Match(someUrl, "[?&]agent_id=(\d+)"; 

if (match.Success) { 
    Session["agent_id"] = match.Groups[1].Value; 
} 
3

我認爲var id = Request["agent_id"]應該工作。

相關問題