I have a problem with getting a 'keyword' from this string, i tried string.replace() but it didn't work, has anyone any idea, how separate keyword from this string?
var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";
Thanks for help!
While you work with xml document, it will be easy to get values elements:
var xml = "<id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword</id>";
var url = XElement.Parse(xml).Value;
var index = url.LastIndexOf('/') + 1;
var keyword = url.Substring(index);
If you always need just last segment you can easily achieve with Url instance:
var keyword = new Uri(url).Segments.Last();
Thanks #Alexei
var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";
string[] splitArra = url.Split(new char[]{'/','<'});
string keywordString = splitArra[11];
I'm sure there's a better and cleaner way to this but this should work:
string keyword = url.Substring((url.IndexOf("videos/")) + 7,url.Length - url.IndexOf("< /id>")+1);
Or this:
string keyword = url.Substring(83, url.Length - url.IndexOf("< /id>") + 1);
Related
I have this url (www.site.com/folder1/sub-folder).
Now I just want to get only folder1 from this url. How can I do that?
I'm getting Last root url from below code but I want only first root url.
string s = Page.Request.Url.AbsolutePath;
s = s.Substring(s.LastIndexOf("/")+1);
Please help me to get only first directory value.
Try this:
Uri uri = new Uri("http://www.example.com/folder1/sub-folder");
var segs = uri.Segments;
var folder = segs[1];
You can use Split() method twice like
string str = "www.site.com/folder1/sub-folder";
string folder = (str.Split('/')[1]).Split('/')[0];
You can use Split like:
var value = s.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
For Example I have this string
http://www.merriam-webster.com/dictionary/sample
What I want is to return only merriam-webster.com I'm planning to use .Replace() but I think there are better approach for this question.
If you are working for Winforms then
string url = "http://www.merriam-webster.com/dictionary/sample";
UriBuilder ub = new UriBuilder(url);
MessageBox.Show(ub.Host.Replace("www.",""));
and for web,
Get host domain from URL?
How about this
System.Uri uri = new Uri("http://stackoverflow.com/search?q=something");
string uriHost = uri.Host;
?
Answers here don't handle the case like "http://abcwww.com".
Check if the url starts with 'www.' before replacing it.
if (url.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
url = url.Replace("www.", string.Empty);
You can use this:
var a = "http://www.merriam-webster.com/dictionary/sample";
var b = a.Split('.');
var c = b[1] +"."+ b[2].Remove(3);
You can leverage System.Uri class:
System.Uri uri = new Uri("https://www.google.com/search?q=something");
string uriWithoutScheme = uri.Host + uri.PathAndQuery + uri.Fragment;
Or you can use below:
UriBuilder ub = new UriBuilder("https://www.google.com/search?q=something");
currentValue = ub.Host.Replace("www.", "");
What is the best way to format the below string in a way so that I can separate out and find the value of PractitionerId, PhysicianNPI, PhysicianName etc.
"PractitionerId:4343343434 , PhysicianNPI: 43434343434, PhysicianName:
John, Doe, PhysicianPhone:2222222222 , PhysicianFax:3333333333 "
So finally I want something like this:
var practitionerId = "4343343434 ";
var physNPI = "43434343434";
var phyName = "John, Doe";
I was thinking of splitting with the names and finding the values assigned to each field but I am not sure if that is the best solution to it.
You could probably generalise this with a regular expression, then use it to build a dictionary/lookup of the terms.
So:
var input= "PractitionerId:4343343434 , PhysicianNPI: 43434343434,"
+ " PhysicianName: John, Doe, PhysicianPhone:2222222222 ,"
+ " PhysicianFax:3333333333";
var pattern = #"(?<=(?<n>\w+)\:)\s*(?<v>.*?)\s*((,\s*\w+\:)|$)";
var dic = Regex
.Matches(input, pattern)
.Cast<Match>()
.ToDictionary(m => m.Groups["n"].Value,
m => m.Groups["v"].Value);
So now you can:
var practitionerId = dic["PractitionerId"];
or
var physicianName = dic["PhysicianName"];
You could get the exact information, doing something like:
var str = "PractitionerId:4343343434 , PhysicianNPI: 43434343434, PhysicianName: John, Doe, PhysicianPhone:2222222222 , PhysicianFax:3333333333 ";
var newStr = str.Split(',');
var practitionerID = newStr[0].Split(':')[1]; // "4343343434"
var physicianNPI = newStr[1].Split(':')[1].Trim(); // "43434343434"
var phyName = newStr[2].Split(':')[1].Trim() + "," + newStr[3]; // "John, Doe"
There are cleaner solutions using Regex patterns though.
Also, you need to parse the corresponding variables to the specific data type you want. Everything here is being treated as a string
Since you seperate information with ",", this should work:
string[] information = yourWholeString.Split(",");
string practitionerId = information[0];
string physNPI = information[1];
string phyName = information[2] + information[3];
I have a text box where I'm getting url, for ex:
http://www.amazon.com/black-series-650.
Now I would like to add /en black-series-650 an it looks like.My ouput should be like this.
http://www.amazon.com/en/black-series-650.
With this code you can apply for url like :
http://www.amazon.com/black-series-650/abc/ed/aaa
You can try this code :
var url = "http://www.amazon.com/black-series-650";
// var url = txt_OriginalUri.Text;
var tempUrl = url.Replace("http://", string.Empty);
var lastSlashIndex = tempUrl.IndexOf('/');
var resultUrl = "http://" + tempUrl.Insert(lastSlashIndex + 1, "en/");
var tmp = OriginalUri.Text;
var en = tmp.Replace("http://www.amazon.com", "http://www.amazon.com/en");
var lst = new List<string>();
lst.Add(OriginalUri.Text);
lst.Add(en);
Instead of dealing urls with Split function you should parse your url using System.Uri. Check the url segments and re-construct as per your need.
This should work for you:
string str = "http://www.amazon.com/black-series-650";
str = str.Insert(str.LastIndexOf("/"),"/en");
You could also try to find the last occurence by using string.LastIndexOf instead of splitting:
string url = txt_OriginalUri.Text;
int idx = url.LastIndexOf('/');
string newUrl = url.Insert(idx,"/en");
I have a simple task without a simple solution.
I have a parameter in the browser that needs to be changed or rewritten
for instance www.contoso.com/countries.aspx?country=UK
all I need is to rewrite the parameter without checking the url so it might appear as:
www.contoso.com/countries.aspx?country=France
I have tried something like that but with no joy
string parameter2 = Request.QueryString["country"];
Context.RewritePath(parameter2.Replace("?country=", "France"));
You could do something like this:
var url = "www.contoso.com/countries.aspx?country={0}";
var country = "UK";
url = String.Format(url, country);
Alternatively you can do:
var url = Request.Url.AbsolutePath;
var country = Request.QueryString["country"];
url = url.Replace(country, "UK");
Then:
Response.Redirect(url);
Can you not read the whole URL into a string, split it on the '?' and then add your new bit to the first part of the string?
Something like this:
var url = Request.QueryString;
var newUrl = url.split('?');
url = newUrl[0] + "?country=France";
I dont know if that will work, its just a thought
If you want to replace the complete querystring, use
newVal = string.LastIndexOf("?");
and then
URL.Replace(oldVal, newVal);
OR if you have just one parameter in querystring and want to replace only value of it, use
newVal = string.LastIndexOf("=");
URL.Replace(oldVal, newVal);
Look at this detailed response for solution to your problem.