C#字符串拆分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1034061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# string split
提问by Goober
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?
操作拆分字符串方法以获得此结果的最简单方法是什么?
采纳答案by Noldorin
回答by grobartn
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
char[] delimiterChars = { ' ' }; // 用于指定更多分隔符 string[] words = full.Split(delimiterChars, 1); // 只用空格分割一个单词
string result = words[0] // this is how you can access it
string result = words[0] // 这是访问它的方式
回答by weiqure
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.
这考虑了子域。
回答by Cody C
Alternate idea
替代想法
string str = "google.com 220 USD 3d 19h";
string match = ".com";
string dotcomportion = str.Substring(str.IndexOf(match), match.Length);
回答by Samuel Carrijo
If by extract you mean remove, you can use the Replace method
如果提取是指删除,则可以使用替换方法
var result = str.Replace(".com", "");
var result = str.Replace(".com", "");
回答by Rony
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));
回答by Paul Alexander
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.
我知道你问过使用 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 );
回答by Andrew Hare
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.
我想不出世界上有什么理由可以让您String.Split
为此目的而使用。这个问题最好用正则表达式解决。
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);
}
}