如何从两个点之间的文本框中获取文本

发布于 2024-07-20 23:28:14 字数 49 浏览 4 评论 0原文

例如,我只想从两个点之间的文本框中获取文本。 万维网。 abc.org 。 H

i just want to get a text from textbox that is betwen two dots for example. www. abc.org . h

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

舂唻埖巳落 2024-07-27 23:28:14

在 C#

string url = "www.google.com";
string[] split_strings = url.Split('.');
Console.WriteLine(split_strings[1]); 

从文本框中获取字符串:

string url = textbox_url.Text;
string[] split_strings = url.Split('.');
Console.WriteLine(split_strings[1]); 

但是请使用 try 和 catch ;)

in C#

string url = "www.google.com";
string[] split_strings = url.Split('.');
Console.WriteLine(split_strings[1]); 

Get String From Textbox:

string url = textbox_url.Text;
string[] split_strings = url.Split('.');
Console.WriteLine(split_strings[1]); 

But please, use try and catch ;)

日久见人心 2024-07-27 23:28:14

我认为你需要更具体地回答你的问题。 现在,如果您只是想提取地址的中间部分,类似下面的内容应该可以完成这项工作:

var parts = textbox.Text.Split(new char[] {'.'});
if (parts.Length < 3) throw new InvalidOperationException("Invalid address.");
var middlePart = parts[1];

You'll need to be a bit more specific with your question I think. Now, if you're just looking to extract the middle part of the address, something like the following should do the job:

var parts = textbox.Text.Split(new char[] {'.'});
if (parts.Length < 3) throw new InvalidOperationException("Invalid address.");
var middlePart = parts[1];
清引 2024-07-27 23:28:14

这与您的要求一样具体吗?

它是否只适用于 www.SOMESITE.com,

其他 TLD 扩展名(如 .net、.org、.co.uk、.ie 等)怎么样?
其他子域(例如 www2.、api.、news.)又如何呢? ETC...
没有子域的域(例如 google.com、theregister.co.uk、bit.ly)怎么样?

如果这很简单,就像您的要求一样,

textBox.Text.Replace("www.", "").Replace(".com", "");

我感觉您没有考虑清楚或完全解释您的要求。

如果是更复杂的场景,您可能需要查看正则表达式。

Is that as specific as your requirement is?

does it only have to work for www.SOMESITE.com

what about other tld extensions like, .net, .org, .co.uk, .ie etc...
what about other subdomains like, www2., api., news. etc...
what about domains with no subdomain like, google.com, theregister.co.uk, bit.ly

if that's a simple as your requirement is,

then

textBox.Text.Replace("www.", "").Replace(".com", "");

though I've a feeling you haven't thought through or fully explained your requirements.

If it is a more complex scenario, you might want to look at Regular expressions.

永言不败 2024-07-27 23:28:14

字符串干草堆=“www.google.com”;
字符串针=“谷歌”;

        string myWord = GetWordFromString(haystack, needle);

        private string GetWordFromString(string haystack, string needle)
        {
           if (haystack.ToLower().Contains(needle))
           {
               return needle;
           }
        }

我重新阅读了带有评论的帖子,我可以看到您可能不知道要提取哪个单词...我认为第一个答案就是您正在寻找的答案。

如果您有特殊需要,还可以使用正则表达式从 URL 中提取域名。
像这样的事情:

    public static string ExtractDomainName(string Url)
    {
        return System.Text.RegularExpressions.Regex.Replace(
        Url,
        @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$",
        "$2"
        );
    } 

string haystack= "www.google.com";
string needle = "google";

        string myWord = GetWordFromString(haystack, needle);

        private string GetWordFromString(string haystack, string needle)
        {
           if (haystack.ToLower().Contains(needle))
           {
               return needle;
           }
        }

I re-read the post with comments I can see that you probably don't know what word you are going to extract... I think the first answer is the one that you are looking fore.

There's also regular expressions for extracting the domainname out of a url if that is your specific need.
Something like this:

    public static string ExtractDomainName(string Url)
    {
        return System.Text.RegularExpressions.Regex.Replace(
        Url,
        @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$",
        "$2"
        );
    } 
暖心男生 2024-07-27 23:28:14
string text = "www. abc.org . h";
int left = Math.Max(text.IndexOf('.'), 0),
   right = Math.Min(text.LastIndexOf('.'), text.Length - 1);

string result = text.Substring(left+1, right - left-1).Trim();
string text = "www. abc.org . h";
int left = Math.Max(text.IndexOf('.'), 0),
   right = Math.Min(text.LastIndexOf('.'), text.Length - 1);

string result = text.Substring(left+1, right - left-1).Trim();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文