简体   繁体   English

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

[英]how to get a text from textbox that is betwen two dots

i just want to get a text from textbox that is betwen two dots for example. 我只想从例如两个点之间的文本框中获取文本。 www. 万维网。 abc.org . abc.org。 h H

in C# 在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 ;) 但是请使用try and catch;)

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];

string haystack= "www.google.com"; 字符串haystack =“ www.google.com”; string needle = "google"; 字符串针=“ 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. 如果您有特殊需要,也可以使用正则表达式从URL中提取域名。 Something like this: 像这样:

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

Is that as specific as your requirement is? 这是否与您的要求一样具体?

does it only have to work for www.SOMESITE.com 它是否仅适用于www.SOMESITE.com

what about other tld extensions like, .net, .org, .co.uk, .ie etc... what about other subdomains like, www2., api., news. 诸如.net,.org,.co.uk,.ie等其他tld扩展名呢?诸如www2。,api。,news等其他子域呢? etc... what about domains with no subdomain like, google.com, theregister.co.uk, bit.ly 等等...没有子域名的域名,例如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. 如果是更复杂的情况,则可能需要查看正则表达式。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM