Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a string with a web address in it which may or may not include the protocol at the beginning. This is going into the href of a html a tag so needs to include the protocol to avoid the address being treated as a relative address. What's the shortest code to achieve this. Just to be clear, the possible inputs and expected outputs are below.
string url = "www.google.com"; //expected "http://www.google.com"
string url = "google.com"; //expected "http://google.com"
string url = "http://www.google.com"; //expected "http://www.google.com"
string url = "https://www.google.com"; //expected "https://www.google.com"
Update:
To those that want to know what I already tried, it was a couple of if statements checking if the url already started with one of the relevant prefixes and then appending it on if necessary. This is trivial for any c# programmer but doesn't come close to the "shortest way". It worked without any problems but my question is to see what better ways there are of doing it.
You can use UriBuilder class for this.
public static Uri GetUri(this string s)
{
return new UriBuilder(s).Uri;
}
This constructor initializes a new instance of the UriBuilder class with the Fragment, Host, Path, Port, Query, Scheme, and Uri properties set as specified in uri.
If uri does not specify a scheme, the scheme defaults to "http:".
This is going into the href of a html a tag so needs to include the protocol to avoid the address being treated as a relative address
You can simply use // to let the browser know it's an absolute url and not a relative path, the browser will then use http or https appropriately (based on the current page context).
For example:
//www.google.com/
Assuming only http/https protocols are expected:
if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
&& !url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
url = "http://" + url;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I need to send a plus(+) operator as string from typescript service to C# Controller. When I debug it, typescript sends as "+", but in C# Controller, the parameter comes as null. The equal sign or negative sign(=,-) are working fine. Just plus sign has a problem. For example if I try to send "13+" plus is recognized as a blank -> "13 " in controller How can I solve it?
+ means space in an URL, so I'm assuming you're assembling an url query string by hand? If so, you need to encode + to %2B.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to extract the url inside the string.
In my case html text is in the db and when i get that text and need to find all url in the text and insert in to another table, can u give me a way to find the url's in SQL or C#.
This is reqular expression to find urls in text
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
One of the possible ways to do it is by using Regular expressions. First option is to extract HTML from the DB, then use Regular Expression to find the links directly. The second option is to locate link tags first, then extract url from them (again by using Regular expressions).
Here you can find information about how to use Regular Expressions in C#:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
On the other hand, writing the correct Regular Expression may not be so easy (it depends on how complex the URL is), but you should take a look at this question: regular expression for url
Also, here you can find a lot of information about regular expressions in general (keep in mind that there are some applications like RegexBuddy, that can help you a lot when it comes to testing your regular expressions): http://www.regular-expressions.info/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working on a program and to avoid complication I need to parse a given string variable to be a DirectoryInfo.
I was wondering if it were possible to parse a string to a DirectoryInfo.
If it is, how does one go about doing that?
Thanks
DirectoryInfo di = new DirectoryInfo(string);
MSDN, linked above, provides you the exceptions in case the string is invalid. Note: this is NOT if the directory exists. MSDN also makes note of this in the Remarks. You must then do:
if(di.Exists)
Well, it's not parsing, but the constructor for DirectoryInfo takes the path as a string:
DirectoryInfo di = new DirectoryInfo(#"c:\MyDir");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I use this code blow in .NET. It works fine. The problem is that I want this audio to play in the root of the website. What changes should I make for this? Thanks
var sample= new System.Windows.Media.MediaPlayer();
sample.Open(new System.Uri( #"D:\voices\1.wav");
sample.Play();
In a web application, this might look something like this:
sample.Open(new System.Uri(Server.MapPath("~/") + #"\voices\1.wav");
I say might because that all depends on whether or not the voices folder exists in the root of the website. Additionally, you should probably leverage Path.Combine instead:
var path = Path.Combine(Server.MapPath("~/"), "voices", "1.wav");
sample.Open(path);
Finally, I don't know what sample is, but the Open method may not work in a website. I'm making the assumption you know what Open does and whether or not it can work in a website.
Use server.mappath("~/") <- that's the filesystem root for your website.
dim path as string = server.mappath("~/") & "/voices/1.wav"
Note that backslashes, for filesystem path, not URI.
Hope it helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following path...
'X:\Projects\4604-Renovation\Unity\4604_02\Assets\Models\FullBuilding\Materials\'
I want to split it at the directory 'Assets' and end up with...
'Assets\Models\FullBuilding\Materials\'
The directory 'Assets' will not always be in the same place in the path. How can I do this? Thanks.
Lets say your string is
string completePath = "X:\Projects\4604-Renovation\Unity\4604_02\Assets\Models\FullBuilding\Materials\";
string subPath = completePath.subString(completePath.IndexOf(#"Assets\"));
Please note that if your path contains multiple instances of Assests it will substring from first instance of Asset.
you can use path.IndexOf, you can use the str.SubString(str.IndexOf("\assetse")), you can do alot of things. playing with string is kinda fun...
most of the things you want to do with strings you can find on google anyway