如何剥离HTTP://和www。使用C#所输入的域名 [英] How to strip http:// and www. from an entered domain name using c#
问题描述
在应用我的工作,我们允许用户输入域名的列表,我们希望用户在任何下列格式
输入域名
但存储这些域名时返回到我们的数据库,我们想将域名存储在以下格式只能
格式:stackoverflow.com
所以,想知道是否有现成的帮手,我可以用它来得到这个工作做好,或者有什么建议这样做有效的方式。
这是我想出了,
公共静态字符串CleanDomainName(字符串域)
{
区= domain.Trim ();
如果('。'。domain.Split()计数()→2)
{
域= domain.Split('。')[1] +。 + domain.Split('。')[2];
}
返回域;
}
请帮我出这一点。
< DIV CLASS =h2_lin>解决方案
使用正则表达式替换字符串开头的表达式:
Regex.Replace(输入,@^(?:HTTP(?:S)://)(?:?WWW(?:[0-9] +)\)?。?字符串.Empty,RegexOptions.IgnoreCase);
这将取代:
- 最终的http://在开始的时候(或https://开头),然后按
- 最终WWW。 (还与多家以下即:www8或www23)
In the application I am working on, we allow user to enter a list of domain names, we expect the user to enter the domain names in any on the following formats
but when storing this domain names back into our databases, we want to store the domain name in the following format only
Format : stackoverflow.com
So wanted to know if there is a ready made helper that I can use to get this job done, or any suggestions to do this in an efficient manner.
This is what I came up with,
public static string CleanDomainName(string domain)
{
domain = domain.Trim();
if (domain.Split('.').Count() > 2)
{
domain = domain.Split('.')[1] + "." + domain.Split('.')[2];
}
return domain;
}
Please help me out on this.
use Regex to replace the expressions in the beginning of the string:
Regex.Replace(input, @"^(?:http(?:s)?://)?(?:www(?:[0-9]+)?\.)?", string.Empty, RegexOptions.IgnoreCase);
this will replace:
- an eventual "http://" in the beginning (or "https://") followed by
- an eventual "www." (also with a number following ie: www8 or www23)
这篇关于如何剥离HTTP://和www。使用C#所输入的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!