views:

333

answers:

5

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

+2  A: 

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

Henrik P. Hessel
I can spot three compiler errors just from glancing at that code!
Noldorin
how to get a text from textbox that is betwen two dots
ankush
do you? ;) should be clear, what to do :)
Henrik P. Hessel
+1  A: 

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];
Noldorin
how to get a text from textbox that is betwen two dots
ankush
That is exactly what the code does. Have you tried it?
Noldorin
A: 

Hi

        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"
        );
    }
The real napster
I think, the google string was just an example, there's probably a need for a generic solution, i.e. by splitting the the string using the '.' as separator, or using real regex
none
how to get a text from textbox that is betwen two dots
ankush
ankush are there always only two dots in your text in the textbox?
The real napster
The first answer by rAyt will solve that. However if someone enters something like "some.funny.looking.string" his code will return "funny" or something like "actually.. go see www.google.com" that will return an empty string.
The real napster
A: 

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.

Eoin Campbell
how to get a text from textbox that is betwen two dots
ankush
A: 
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();
Ihar Voitka