5

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?

asked Jun 23, 2009 at 17:45
6
  • 11
    string extracted = ".com";. That's the easiest way, and you don't even need to use the string.split! :-) Commented Jun 23, 2009 at 17:48
  • 1
    Not enough detail - if you supply further examples of strings and what you need to extract, a good general solution can be devised. Commented Jun 23, 2009 at 17:49
  • Consider using a regular expression. Commented Jun 23, 2009 at 17:51
  • 4
    @Jay Riggs - now you have two problems. Commented Jun 23, 2009 at 17:57
  • 1
    @Jay Riggs - famous quote from Jamie Zawinski, check out the link: codinghorror.com/blog/archives/001016.html Commented Jun 23, 2009 at 18:10

8 Answers 8

8

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
Fredrik Mörk
159k31 gold badges296 silver badges349 bronze badges
answered Jun 23, 2009 at 17:46
Sign up to request clarification or add additional context in comments.

6 Comments

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
I assume var tld = domain.Substring(domain.IndexOf('.')) was meant (edited it in; hope you don't mind Noldorin)?
@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
@Fredrik: Thanks. And yeah, always feel free when it's an obvious mistake (caused by typing in a rush in this case). :)
@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.
|
3

Alternate idea

string str = "google.com 220 USD 3d 19h";
string match = ".com";
string dotcomportion = str.Substring(str.IndexOf(match), match.Length);
answered Jun 23, 2009 at 17:50

2 Comments

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: 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).
1

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

answered Jun 23, 2009 at 17:48

Comments

1

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

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

answered Jun 23, 2009 at 17:54

Comments

1

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 );
answered Jun 23, 2009 at 17:55

Comments

1

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.

answered Jun 23, 2009 at 17:49

Comments

0

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));
answered Jun 23, 2009 at 17:55

Comments

0

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);
 }
}
answered Jun 23, 2009 at 18:02

3 Comments

Downvoters - is there a reason why this was downvoted (other than the fact that I didn't use String.Split)?
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.
Hmm, good point - I do sound like a bit of a jerk :) Thanks for pointing that out.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.