how do i extract the last 5 digits in C# of a link, for example
www.mywebsite.com?agent_id=12345
now I want to save the 12345 in a session variable
Session["agent_id"] = ???
I think var id = Request["agent_id"] should work.
If you are trying to get the agent_id value from the current request, then Marcos' answer will get you what you want.
However, if the "link" you are referring to is in some other string variable, your best bet is to use regular expressions.
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;
}
You can use the query string property for that
string agentId = Request.QueryString["agent_id"];
more info for QueryString here
Related
I need to get a certain part from a GET request query string. For example, if the query string is:
action=balance&id=123&session_id=123&key=3843
I would like to convert it to
action=balance&id=123&session_id=123
i.e. I would like to cut off the key parameter part. How could I do that?
After a short googling, I've found the answer here: https://www.codeproject.com/Tips/574956/How-to-get-URL-and-QueryString-value-in-an-ASP-NET
In short, you are able to retrieve what you are looking for by using the following method:
Request.ServerVariables("QUERY_STRING")
use the substring function in your language of choice
for eg. in javascript you can do it as
var f = "action=balance&id=123&session_id=123&key=3843";
var a = f.replace(f.substring(f.indexOf('&key')),"");
There are lot of other ways also.
Try
Regex.Replace(Request.RawUrl, #"&key.*", "")
string qs = Request.params;
This will return collection of all params in curre
Get the Last Index Of char '&'. Then get the substring.
string url = Request.params;
int index = url.LastIndexOf('&');
var urlWithOutKey = url.Substring(0, index);
I have investigated a little bit more and find out this solution
string url = Request.RequestUri.ToString();
Uri uri = new Uri(url);
string urlStringQuery = uri.Query;
int endIndex = urlStringQuery.IndexOf("&key", 1);
string query = urlStringQuery.Substring(1, endIndex - 1);
Hi all I want to know something regarding to fixed-string in regular expression.
How to represent a fixed-string, regardless of special characters or alphanumeric in C#?
For eg; have a look at the following string:
infinity.world.uk/Members/namelist.aspx?ID=-1&fid=X
The entire string before X will be fixed-string (ie; the whole sentence will appear the same) BUT only X will be the decimal variable.
What I want is that I want to append decimal number X to the fixed string. How to express that in terms of C# regular expression.
Appreciate your help
string fulltext = "inifinity.world.uk/Members/namelist.aspx?ID=-1&fid=" + 10;
if you need to modify existing url, dont use regex, string.Format or string.Replace you get problem with encoding of arguments
Use Uri and HttpUtility instead:
var url = new Uri("http://infinity.world.uk/Members/namelist.aspx?ID=-1&fid=X");
var query = HttpUtility.ParseQueryString(url.Query);
query["fid"] = 10.ToString();
var newUrl = url.GetLeftPart(UriPartial.Path) + "?" + query;
result: http://infinity.world.uk/Members/namelist.aspx?ID=-1&fid=10
for example, using query["fid"] = "%".ToString(); you correctly generate http://infinity.world.uk/Members/namelist.aspx?ID=-1&fid=%25
demo: https://dotnetfiddle.net/zZ9Y1h
String.Format is one way of replacing token values in a string, if that's what you want. In the example below, the {0} is a token, and String.Format takes the fixedString and replaces the token with the value of myDecimal.
string fixedString = "infinity.world.uk/Members/namelist.aspx?ID=-1&fid={0}";
decimal myDecimal = 1.5d;
string myResultString = string.Format(fixedString, myDecimal.ToString());
I'm a beginner in C# and I have the following string,
string url = "svn1/dev";
along with,
string urlMod = "ato-svn3-sslv3.of.lan/svn/dev"
I want to replace svn1 in url with "ato-svn3-sslv3.of.lan"
Although your question still has some inconsistent statements, I believe String.Replace is what you are looking for:
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
url = url.Replace("svn1","ato-svn3-sslv3.of.lan");
Strings are immutable so you need to assign the return value to a variable:
string replacement = "ato-svn3-sslv3.of.lan";
url = url.Replace("svn1", replacement);
You can use the string method replace.
url = url.Replace("svn1", urlMod)
I think you need this:
string url = "svn1/dev";
string anotherUrl = "ato-svn3-sslv3.of.lan/svn/dev";
string toBeReplaced = anotherUrl.Split('/')[0];
url = url.Replace("svn1", toBeReplaced);
It uses split method and replace method.
I am Searching though the contents of a Excel Spreadsheet template at replacing all the variables represented by #Something.
I have all the variables names come up with code, i'm just looking for a way to replace the them in a string.
Example:
My name is #Name
I want it to become:
My name is John.
My Code explained:
I have an array holding a struct which holds:
TagPointer: this is my variable #name,
TagValue: this would be the name John
Here i can get the index of where my variable is and i have the original String its Comp.
I am just unsure how i can replace the #Something.
int testnum;
if (Comp != "null")
{
if (Comp.IndexOf(ArrayNode[i].TagPointer) != -1)
{
testnum = Comp.IndexOf(ArrayNode[i].TagPointer);
}
}
use string.Format(), string.Replace() there is less convenient
string value = "John";
string result = string.Format("My name is {0}", value);
Unless I'm missing something, can't you use the string.replace function?
You could do something like:
foreach (var tag in ArrayNode)
{
Comp = Comp.Replace(tag.TagPointer, tag.TagValue);
}
Have you ever seen the FormatWith extension?
With it you can write something like this:
Status.Text = "{UserName} last logged in at {LastLoginDate}".FormatWith(user);
Why not using
ie
string name = "Giusepe";
string _tmp = "My name is #Name";
_tmp = _tmp.Replace("#Name", name);
You could iterate over your array and use the .Contains() to see which tag you have present and use the .Replace() to replace the tag with the actual value.
Do you want this:
string a = "#Something";
a = a.Replace("#somethig", Value);
?
Years later, we now have string interpolation to help with this.
I want to get only number id from string. result : 123456
var text = "http://test/test.aspx?id=123456dfblablalab";
EDIT:
Sorry, Another number can be in the text. I want to get first number after id.
var text = "http://test/test.aspx?id=123456dfbl4564dsf";
Use:
Regex.Match(text, #"id=(\d+)").Groups[1].Value;
It depends on the context - in this case it looks like you are parsing a Uri and a query string:
var text = "http://test/test.aspx?id=123456dfblablalab";
Uri tempUri = new Uri(text);
NameValueCollection query = HttpUtility.ParseQueryString(tempUri.Query);
int number = int.Parse(new string(query["id"].TakeWhile(char.IsNumber).ToArray()));
Someone will give you a C# implementation, but it's along the lines of
/[\?\&]id\=([0-9]+)/
Which will match either &id=123456fkhkghkf or ?id=123456fjgjdfgj (so it'll get the value wherever it is in the URL) and capture the number as a match.