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?
-
11string extracted = ".com";. That's the easiest way, and you don't even need to use the string.split! :-)Paul Sonier– Paul Sonier2009年06月23日 17:48:32 +00:00Commented Jun 23, 2009 at 17:48
-
1Not enough detail - if you supply further examples of strings and what you need to extract, a good general solution can be devised.Oded– Oded2009年06月23日 17:49:40 +00:00Commented Jun 23, 2009 at 17:49
-
Consider using a regular expression.Jay Riggs– Jay Riggs2009年06月23日 17:51:30 +00:00Commented Jun 23, 2009 at 17:51
-
4@Jay Riggs - now you have two problems.Matthew Jones– Matthew Jones2009年06月23日 17:57:56 +00:00Commented Jun 23, 2009 at 17:57
-
1@Jay Riggs - famous quote from Jamie Zawinski, check out the link: codinghorror.com/blog/archives/001016.htmlMatthew Jones– Matthew Jones2009年06月23日 18:10:48 +00:00Commented Jun 23, 2009 at 18:10
8 Answers 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
6 Comments
Alternate idea
string str = "google.com 220 USD 3d 19h";
string match = ".com";
string dotcomportion = str.Substring(str.IndexOf(match), match.Length);
2 Comments
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
Comments
If by extract you mean remove, you can use the Replace method
var result = str.Replace(".com", "");
Comments
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 );
Comments
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.
Comments
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));
Comments
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);
}
}