C# 如何将文本(“https://”)添加到不是以https://开头而是以www开头的字符串中?

C# 如何将文本(“https://”)添加到不是以https://开头而是以www开头的字符串中?,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我收到数据,并且我的标签(命名网站)有以https开头和不以https开头的url,因此当标签以www而不是http开头时,我希望将其添加到字符串中,以便链接对那些没有将https放入url链接的人起作用 这就是我目前拥有的: URLButton.Clicked += (object sender, EventArgs e) => { Uri outvalue; if(Uri.TryCreate(website.Text, UriKind.Absolute, out

我收到数据,并且我的标签(命名网站)有以https开头和不以https开头的url,因此当标签以www而不是http开头时,我希望将其添加到字符串中,以便链接对那些没有将https放入url链接的人起作用

这就是我目前拥有的:

URLButton.Clicked += (object sender, EventArgs e) => { 

    Uri outvalue;

    if(Uri.TryCreate(website.Text, UriKind.Absolute, out outvalue))
    {
        Device.OpenUri(outvalue);
    }
};
使用此当前代码,开始时没有http但有www的URL将不可单击

因此,如果一个网站URL没有https(就像他们中的一些没有),我需要在代码中手动添加https,但我不知道如何才能做到这一点

If (website.text != (start with http))
"https://" + website.text

else 

 website.text = website.text or something like that
暴力:

if (!website.Text.StartsWith("https://") ) {
                website.Text= "https://" + website.Text;
            }
根据可能的输入排列,可能需要更可靠的验证。

暴力:

if (!website.Text.StartsWith("https://") ) {
                website.Text= "https://" + website.Text;
            }

可能需要根据可能的输入排列进行更可靠的验证。

要清楚,您要做的是将类似
www.google.se
的内容转换为
http:\\www.google.se

if(!website.Text.StartsWith("http://"))
{
   website.Text ="http://" + website.Text;
}

需要明确的是,您要做的是将类似
www.google.se
的内容转换为
http:\\www.google.se

if(!website.Text.StartsWith("http://"))
{
   website.Text ="http://" + website.Text;
}

更优雅的解决方案是使用
UriBuilder
类(它还可以添加缺少的
www.
):


更优雅的解决方案是使用
UriBuilder
类(它还可以添加缺少的
www.
):


的确更优雅。好的,这样做更优雅。好办法