tags:

views:

767

answers:

8
+2  Q: 

C# string split

Hello! I have a string = "google.com 220 USD 3d 19h".

I want to extract just the ".com" part.......

whats the easiest way to manipulate the split string method to get this result?

+5  A: 

I'm guessing you either want to extract the domain name or the TLD part of the string. This should do the job:

var str = "google.com 220 USD 3d 19h";
var domain = str.Split(' ')[0];           // google.com
var tld = domain.Substring(domain.IndexOf('.')) // .com
Noldorin
Not sure why this is voted up, the tld code is wrong. I am guessing you meant it to say: var tld = domain.Substring(domain.IndexOf('.')); // .com
RedFilter
I assume var tld = domain.Substring(domain.IndexOf('.')) was meant (edited it in; hope you don't mind Noldorin)?
Fredrik Mörk
@OrbMan: You're an awfully harsh judge! It was clearly just a typo, which you could have fixed yourself. I assume the down vote came from you. :P
Noldorin
@Fredrik: Thanks. And yeah, always feel free when it's an obvious mistake (caused by typing in a rush in this case). :)
Noldorin
@Noldorin - maybe I don't know the protocol around here, I thought wrong answers should be voted down, and I've never edited answers before, I tend to tell people and wait for them to fix. Vote restored.
RedFilter
@OrbMan: Well, generally answers that recommend a bad approach or contain incorrect theory/explanation may be voted down. Similarly, answers that contain a significant amount of nonsensical code. Relatively small mistakes in code (ones that could be accidental) should however either be pointed out to the poster or fixed, without any vote. Hope that makes sense. Anyway, thanks for restoring it.
Noldorin
A: 

well if you can assume that space is seperator its as easy as

string full

char[] delimiterChars = { ' ' }; // used so you can specify more delims string[] words = full.Split(delimiterChars, 1); // splits only one word with space

string result = words[0] // this is how you can access it

grobartn
A: 

Assuming you want the top-level domain:

string str = "google.com 220 USD 3d 19h";
string tld = str.Substring(str.LastIndexOf('.')).Split(' ')[0];
Console.WriteLine(tld);

Output:

.com

This takes subdomains into account.

+1  A: 

Alternate idea

string str = "google.com 220 USD 3d 19h";
string match = ".com";
string dotcomportion = str.Substring(str.IndexOf(match), match.Length);
Cody C
This only works if the TLD is .com. Considering that the point is to actually find out what the tld is, this seems odd.
Brian
@Brian: while I agree that the code is not the right solution, it does pick out any three-letter TLD (such as .net or .org). It will however miss any other TLD (such as .de or .info).
Fredrik Mörk
A: 

If by extract you mean remove, you can use the Replace method

var result = str.Replace(".com", "");

Samuel Carrijo
A: 

using Regex would be the best option but if you want to use Split then

  var str = "google.com 220 USD 3d 19h";
        var str1  = str.Split(' ')[0];
        var str2 = str1.Split('.')[0];
        Console.WriteLine(str1.Replace(str2, string.Empty));
Rony
A: 

I know you asked about using the Split method but I'm not sure that's the best route. Splitting a string will allocate at least 5 new strings that are immediately ignored and then have to wait around until GC to be released. You're better off just using indexing into the string and pull out just what you need.

string str =  "google.com 220 USD 3d 19h";
int ix = str.IndexOf( ' ' );
int ix2 = str.IndexOf( '.', 0, ix );
string tld = str.Substring( ix2, ix - ix2 );
string domain = str.Substring( 0, ix );
Paul Alexander
A: 

I cannot think of a reason in the world that you would want to use String.Split for this purpose. This problem is best solved with a regular expression.

Here is a small program that demonstrates how to do it:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
     String foo = "google.com 220 USD 3d 19h";
     Regex regex = new Regex(@"(.com)", RegexOptions.IgnoreCase);
     Match match = regex.Match(foo);

     if (match.Success)
      Console.WriteLine(match.Groups[1].Value);
    }
}
Andrew Hare
Downvoters - is there a reason why this was downvoted (other than the fact that I didn't use String.Split)?
Andrew Hare
I didn't downvote, but your tone may have something to do with it. Just because you can't think of a reason doesn't mean there isn't one.
Jeff Yates
Hmm, good point - I do sound like a bit of a jerk :) Thanks for pointing that out.
Andrew Hare