在 C# 中用另一个字符串分割一个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2245442/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:43:35  来源:igfitidea点击:

Split a string by another string in C#

c#.netstringsplit

提问by Brandon

I've been using the Split()method to split strings, but this only appears to work if you are splitting a string by a character. Is there a way to split a string, with another string being the split by parameter?

我一直在使用该Split()方法来拆分字符串,但这仅在您按字符拆分字符串时才有效。有没有办法拆分 a string,另一个字符串是按参数拆分的?

I've tried converting the splitter into a character array, with no luck.

我试过将拆分器转换为字符数组,但没有成功。

In other words, I'd like to split the string:

换句话说,我想拆分string

THExxQUICKxxBROWNxxFOX

THExxQUICKxxBROWNxxFOX

by xx, and return an array with values:

by xx,并返回一个包含值的数组:

THE, QUICK, BROWN, FOX

THE、QUICK、BROWN、FOX

采纳答案by Adam Robinson

In order to split by a string you'll have to use the string array overload.

为了按字符串拆分,您必须使用字符串数组重载

string data = "THExxQUICKxxBROWNxxFOX";

return data.Split(new string[] { "xx" }, StringSplitOptions.None);

回答by Peter

Regex.Split(string, "xx")

is the way I do it usually.

是我通常这样做的方式。



Of course you'll need:

当然你需要:

using System.Text.RegularExpressions;

or :

或者 :

System.Text.RegularExpressions.Regex.Split(string, "xx")

but then again I need that library all the time.

但话说回来,我一直都需要那个图书馆。

回答by Greg

There is an overload of Splitthat takes strings.

有一个带字符串的 Split 重载

"THExxQUICKxxBROWNxxFOX".Split(new [] { "xx" }, StringSplitOptions.None);

You can use either of these StringSplitOptions

您可以使用这些 StringSplitOptions 中的任何一个

  • None- The return value includes array elements that contain an empty string
  • RemoveEmptyEntries- The return value does not include array elements that contain an empty string
  • - 返回值包括包含空字符串的数组元素
  • RemoveEmptyEntries- 返回值不包括包含空字符串的数组元素

So if the string is "THExxQUICKxxxxBROWNxxFOX", StringSplitOptions.Nonewill return an empty entry in the array for the "xxxx" part while StringSplitOptions.RemoveEmptyEntrieswill not.

因此,如果字符串是“THExxQUICKxxxxBROWNxxFOX”,StringSplitOptions.None将在数组中为“xxxx”部分返回一个空条目,而StringSplitOptions.RemoveEmptyEntries不会。

回答by bruno conde

There's an overload of String.Splitfor this:

为此有一个String.Split的重载:

"THExxQUICKxxBROWNxxFOX".Split(new [] {"xx"}, StringSplitOptions.None);

回答by Lorenz Lo Sauer

I generally like to use my own extension for that:

我通常喜欢使用我自己的扩展:

string data = "THExxQUICKxxBROWNxxFOX";
var dataspt = data.Split("xx");
//>THE  QUICK  BROWN  FOX 


//the extension class must be declared as static
public static class StringExtension
{   
    public static string[] Split(this string str, string splitter)
    {
        return str.Split(new[] { splitter }, StringSplitOptions.None);
    }
}

This will however lead to an Exception, if Microsoft decides to include this method-overload in later versions. It is also the likely reason why Microsoft has not included this method in the meantime: At least one company I worked for, used such an extension in all their C# projects.

但是,如果 Microsoft 决定在以后的版本中包含此方法重载,这将导致异常。这也是微软在此期间没有包含这种方法的可能原因:至少我工作过的一家公司,在他们所有的 C# 项目中都使用了这样的扩展。

It may also be possible to conditionally define the method at runtime if it doesn't exist.

如果方法不存在,也可以在运行时有条件地定义该方法。

回答by user3458227

The easiest way is to use String.Replace:

最简单的方法是使用String.Replace

string myString = "THExxQUICKxxBROWNxxFOX";
mystring = mystring.Replace("xx", ", ");

Or more simply:

或者更简单:

string myString = "THExxQUICKxxBROWNxxFOX".Replace("xx", ", ");

回答by SNag

string data = "THExxQUICKxxBROWNxxFOX";

return data.Replace("xx","|").Split('|');

Just choose the replace character carefully (choose one that isn't likely to be present in the string already)!

只需仔细选择替换字符(选择一个不太可能出现在字符串中的字符)!

回答by argyle

The previous answers are all correct. I go one step further and make C# work for me by defining an extension methodon String:

前面的答案都是正确的。我更进一步,通过在 String 上定义扩展方法使 C# 为我工作:

public static class Extensions
{
    public static string[] Split(this string toSplit, string splitOn) {
        return toSplit.Split(new string[] { splitOn }, StringSplitOptions.None);
    }
}

That way I can call it on any string in the simple way I naively expected the first time I tried to accomplish this:

这样我就可以以我第一次尝试完成此操作时天真地预期的简单方式在任何字符串上调用它:

"a big long string with stuff to split on".Split("g str");

回答by user890255

This is also easy:

这也很简单:

string data = "THExxQUICKxxBROWNxxFOX";
string[] arr = data.Split("xx".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

回答by Danation

As of .NET Core 2.0, there is an override that takes a string.

从 .NET Core 2.0 开始,有一个采用字符串的覆盖。

So now you can do "THExxQUICKxxBROWNxxFOX".Split("xx").

所以现在你可以做"THExxQUICKxxBROWNxxFOX".Split("xx")

See https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netcore-2.0#System_String_Split_System_String_System_StringSplitOptions_

请参阅https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netcore-2.0#System_String_Split_System_String_System_StringSplitOptions_