I have a line = "google.com 220 USD 3d 19h".
I want to extract only a part of ".com" .......
What is an easy way to manipulate the split string method to get this result?
I assume that you either want to extract the domain name or TLD part of the string. This should accomplish this task:
var str = "google.com 220 USD 3d 19h"; var domain = str.Split(' ')[0]; // google.com var tld = domain.Substring(domain.IndexOf('.')) // .com
Alternative idea
string str = "google.com 220 USD 3d 19h"; string match = ".com"; string dotcomportion = str.Substring(str.IndexOf(match), match.Length);
, , seperator ,
char [] delimiterChars = {''};//, string [] words = full.Split(delimiterChars, 1);//
string result = words [0]// ,
, :
string str = "google.com 220 USD 3d 19h"; string tld = str.Substring(str.LastIndexOf('.')).Split(' ')[0]; Console.WriteLine(tld);
:
.com
.
remove, Replace
var result = str.Replace( ". com", "");
, Split, , . 5 , , , GC. , .
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 );
Regex , Split,
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));
I cannot think of a reason in the world that you would like to use for this purpose String.Split. This problem is best solved with regex.
String.Split
Here is a small program that demonstrates how to do this:
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); } }