C # line splitting

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?

+5
source share
8 answers

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
+8
source

Alternative idea

string str = "google.com 220 USD 3d 19h";
string match = ".com";
string dotcomportion = str.Substring(str.IndexOf(match), match.Length);
+3
source

, , seperator ,

char [] delimiterChars = {''};//, string [] words = full.Split(delimiterChars, 1);//

string result = words [0]// ,

+1

, :

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

:

.com

.

+1

remove, Replace

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

+1

, 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 );
+1

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

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.

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);
    }
}
0
source

All Articles