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.
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);
I have an application where uses post comments. Security is not an issue.
string url = http://example.com/xyz/xyz.html?userid=xyz&comment=Comment
What i want is to extract the userid and comment from above string.
I tried and found that i can use IndexOf and Substring to get the desired code BUT what if the userid or comment also has = symbol and & symbol then my IndexOf will return number and my Substring will be wrong.
Can you please find me a more suitable way of extracting userid and comment.
Thanks.
I got url using string url =
HttpContext.Current.Request.Url.AbsoluteUri;
Do not use AbsoluteUri property , it will give you a string Uri, instead use the Url property directly like:
var result = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.Url.Query);
and then you can extract each parameter like:
Console.WriteLine(result["userid"]);
Console.WriteLine(result["comment"]);
For other cases when you have string uri then do not use string operations, instead use Uri class.
Uri uri = new Uri(#"http://example.com/xyz/xyz.html?userid=xyz&comment=Comment");
You can also use TryCreate method which doesn't throw exception in case of invalid Uri.
Uri uri;
if (!Uri.TryCreate(#"http://example.com/xyz/xyz.html?userid=xyz&comment=Comment", UriKind.RelativeOrAbsolute, out uri))
{
//Invalid Uri
}
and then you can use System.Web.HttpUtility.ParseQueryString to get query string parameters:
var result = System.Web.HttpUtility.ParseQueryString(uri.Query);
The ugliest way is the following:
String url = "http://example.com/xyz/xyz.html?userid=xyz&comment=Comment";
usr = url.Split('?')[1];
usr= usr.Split('&')[0];
usr = usr.Split('=')[1];
But #habib version is better
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 have a winform application, and I would like to parse a string that represent an URL to extract some parameters.
a sample of the URL is this:
http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e
the parameter I would like to extract is 271443634510 (that is, the last part of the path before the query string).
Any idea ho how this can be done?
You can use Uri.Segments, which splits up the stuff after your domain into an array that includes, for your example:
/
itm/
Sector-Watch/
271443634510
So all you need to get is the item at index 3. Working example:
string url = "http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
Uri uri = new Uri(url);
var whatYouWant = uri.Segments[3];
You can do this:
string url = "http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
string parameter = Regex.Match(url,"\d+(?=\?)|(?!/)\d+$").Value;
You can simply use Split function (tested and verified):
string MyUrl="http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
string str=MyUrl.Split('/').Last().Split('?').First();
How to get text before a symbol in string ? Any ideas?
e.g. acsbkjb/123kbvh/123jh/
get text before first - "/"
Try this
string ss = myString.Split('/')[0];
You can use Substring() method to get the required part of the string.
String text="acsbkjb/123kbvh/123jh/";
int index=text.IndexOf('/');
String text2="";
if(index>=0)
text2=text.Substring(0,index);
get substring like
youstring.Substring(0,yourstring.IndexOf('/'));
The IEnumerable approach
string str = "acsbkjb/123kbvh/123jh/";
var result = new string(str.TakeWhile(a => a != '/').ToArray());
Console.WriteLine(result);
If there are no forward slashes this works without need to check the return of IndexOf
EDIT Keep this answer just as an example because the efficiency of this approach is really worse. IndexOf works faster also if you add an if statement to check the return value.
string text = "acsbkjb/123kbvh/123jh/";
string text2 = text.Substring(0, text.IndexOf("/"));