简体   繁体   English

C#字符串拆分

[英]C# string split

I have a string = "google.com 220 USD 3d 19h". 我有一个字符串=“google.com 220 USD 3d 19h”。

I want to extract just the ".com" part....... 我想提取“.com”部分.......

whats the easiest way to manipulate the split string method to get this result? 什么是最简单的方法来操纵split string方法来获得这个结果?

I'm guessing you either want to extract the domain name or the TLD part of the string. 我猜你要么提取字符串的域名或TLD部分。 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

Alternate idea 替代的想法

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

well if you can assume that space is seperator its as easy as 好吧,如果你可以假设空间是分离器就像它一样容易

string full 满满的

char[] delimiterChars = { ' ' }; char [] delimiterChars = {''}; // used so you can specify more delims string[] words = full.Split(delimiterChars, 1); //使用所以你可以指定更多delims string [] words = full.Split(delimiterChars,1); // splits only one word with space //只用空格分割一个单词

string result = words[0] // this is how you can access it string result = words [0] //这是你可以访问它的方法

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. 这会考虑子域。

If by extract you mean remove, you can use the Replace method 如果通过提取意味着删除,则可以使用Replace方法

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

I know you asked about using the Split method but I'm not sure that's the best route. 我知道你问过使用Split方法,但我不确定这是最好的路线。 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. 拆分字符串将分配至少5个立即被忽略的新字符串,然后必须等待,直到GC被释放。 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 );

using Regex would be the best option but if you want to use Split then 使用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 want to use String.Split for this purpose. 我想不出世界上你想要将String.Split用于此目的的原因。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM