C# 如何使用字符串分隔符拆分字符串?

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

How can I split a string with a string delimiter?

c#stringsplit

提问by markzzz

I have this string:

我有这个字符串:

My name is Marco and I'm from Italy

I'd like to split it, with delimiter is Marco and, so I should get an array with

我想用 delimiter 分割它is Marco and,所以我应该得到一个数组

  • My nameat [0] and
  • I'm from Italyat [1].
  • My name在 [0] 和
  • I'm from Italy在 [1]。

How can I do it with C#?

我怎样才能用 C# 做到这一点?

I tried with:

我试过:

.Split("is Marco and")

But it wants only a single char.

但它只需要一个字符。

采纳答案by juergen d

string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);

If you have a single character delimiter (like for instance ,), you can reduce that to (note the single quotes):

如果您有一个单字符分隔符(例如,),您可以将其减少为(注意单引号):

string[] tokens = str.Split(',');

回答by Patrick

You could use the IndexOfmethod to get a location of the string, and split it using that position, and the length of the search string.

您可以使用该IndexOf方法获取字符串的位置,并使用该位置和搜索字符串的长度将其拆分。



You can also use regular expression. A simple google searchturned out with this

您也可以使用正则表达式。一个简单的谷歌搜索结果是这样的

using System;
using System.Text.RegularExpressions;

class Program {
  static void Main() {
    string value = "cat\r\ndog\r\nanimal\r\nperson";
    // Split the string on line breaks.
    // ... The return value from Split is a string[] array.
    string[] lines = Regex.Split(value, "\r\n");

    foreach (string line in lines) {
        Console.WriteLine(line);
    }
  }
}

回答by Anders Tornblad

.Split(new string[] { "is Marco and" }, StringSplitOptions.None)

Consider the spaces surronding "is Marco and". Do you want to include the spaces in your result, or do you want them removed? It's quite possible that you want to use " is Marco and "as separator...

考虑周围的空间"is Marco and"。您要在结果中包含空格,还是要删除它们?您很可能想" is Marco and "用作分隔符...

回答by Charles Lambert

There is a version of string.Splitthat takes an array of strings and a StringSplitOptionsparameter:

有一个版本string.Split需要一个字符串数组和一个StringSplitOptions参数:

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

回答by DanTheMan

Try this functioninstead.

试试这个功能吧

string source = "My name is Marco and I'm from Italy";
string[] stringSeparators = new string[] {"is Marco and"};
var result = source.Split(stringSeparators, StringSplitOptions.None);

回答by Huusom

You are splitting a string on a fairly complex sub string. I'd use regular expressions instead of String.Split. The later is more for tokenizing you text.

您在相当复杂的子字符串上拆分字符串。我会使用正则表达式而不是 String.Split。后者更多地用于标记您的文本。

For example:

例如:

var rx = new System.Text.RegularExpressions.Regex("is Marco and");
var array = rx.Split("My name is Marco and I'm from Italy");

回答by Guillaume Slashy

Read C# Split String Examples - Dot Net Pearlsand the solution can be something like:

阅读C# 拆分字符串示例 - 点网珍珠,解决方案可能类似于:

var results = yourString.Split(new string[] { "is Marco and" }, StringSplitOptions.None);