如何在c#中拆分此字符串?

时间:2021-11-18 01:37:27

i'm really not used to the split string method in c# and i was wondering how come there's no split by more than one char function?

我真的不习惯c#中的split string方法,我想知道怎么没有多个char函数分割?

and my attempt to try to split this string below using regex has just ended up in frustration. anybody can help me?

我尝试使用正则表达式尝试将此字符串拆分为下方,但最终却陷入了挫败感。有人可以帮帮我吗?

basically i want to split the string below down to

基本上我想将下面的字符串拆分为

aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^

into

aa**aa**bb**dd
a2a**a2a**b2b**dd

and then later into

然后进入

aa
aa
bb
dd

a2a
a2a
b2b
dd

thanks!

7 个解决方案

#1


10  

You can split using a string[]. There are several overloads.

您可以使用字符串[]进行拆分。有几个重载。

string[] splitBy = new string[] {"^__^"};
string[] result = "aa*aa*bb*dd^__^a2a*a2a*b2b*dd^__^".Split(splitBy, StringSplitOptions.None);

// result[0] = "aa*aa*bb*dd", result[1] = "a2a*a2a*b2b*dd"

#2


7  

You're looking for this overload:

你正在寻找这个重载:

someString.Split(new string[] { "^__^" }, StringSplitOptions.None);

I've never understood why there isn't a String.Split(params string[] separators).
However, you can write it as an extension method:

我永远不明白为什么没有String.Split(params string []分隔符)。但是,您可以将其编写为扩展方法:

public static string[] Split(this string str, params string[] separators) {
    return str.Split(separators, StringSplitOptions.None);
}

#3


2  

var split = @"aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^".Split(new string[] {"^__^"}, StringSplitOptions.RemoveEmptyEntries);

foreach(var s in split){
   var split2 = s.Split(new string[] {"**"}, StringSplitOptions.RemoveEmptyEntries);
}

#4


1  

This will give you an IEnumerable<IEnumerable<string>> containing the values you want.

这将为您提供包含所需值的IEnumerable >。

string[] split1 = new[] { "^__^" };
string[] split2 = new[] { "**" };
StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
var vals = s.Split(split1,op).Select(p => p.Split(split2,op).Cast<string>());

You can also throw some ToList()'s in there if you wanted List<List<string>> or ToArray()'s for a jagged array

如果你想要一个锯齿状数组的List >或ToArray(),你也可以在那里抛出一些ToList()

#5


0  

How about one of these:

其中一个怎么样:

string.Join("**",yourString.Split("^__")).Split("**")
yourString.Replace("^__","**").Split("**")

You will have to check the last item, in your example it would be an empty string.

你必须检查最后一项,在你的例子中它将是一个空字符串。

#6


0  

As already stated, you can pass in a string array to the Split method. Here's how the code might look based on your recent edits to the quesiton:

如前所述,您可以将字符串数组传递给Split方法。以下是基于您最近对问题的编辑,代码的外观:

string x = "aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^";
string[] y =  x.Split(new string[]{"^__^"},StringSplitOptions.None);
string[] z = y[0].Split(new string[]{"**"},StringSplitOptions.None);

#7


0  

The VB.NET Version that works.

有效的VB.NET版本。

Module Module1

Sub Main()
    Dim s As String = "aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^"
    Dim delimit As Char() = New Char() {"*"c, "^"c, "_"c}

    Dim split As String() = s.Split(delimit, StringSplitOptions.RemoveEmptyEntries)
    Dim c As Integer = 0
    For Each t As String In split
        c += 1
        Console.WriteLine(String.Format("{0}: {1}", c, t))
    Next

End Sub

#1


10  

You can split using a string[]. There are several overloads.

您可以使用字符串[]进行拆分。有几个重载。

string[] splitBy = new string[] {"^__^"};
string[] result = "aa*aa*bb*dd^__^a2a*a2a*b2b*dd^__^".Split(splitBy, StringSplitOptions.None);

// result[0] = "aa*aa*bb*dd", result[1] = "a2a*a2a*b2b*dd"

#2


7  

You're looking for this overload:

你正在寻找这个重载:

someString.Split(new string[] { "^__^" }, StringSplitOptions.None);

I've never understood why there isn't a String.Split(params string[] separators).
However, you can write it as an extension method:

我永远不明白为什么没有String.Split(params string []分隔符)。但是,您可以将其编写为扩展方法:

public static string[] Split(this string str, params string[] separators) {
    return str.Split(separators, StringSplitOptions.None);
}

#3


2  

var split = @"aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^".Split(new string[] {"^__^"}, StringSplitOptions.RemoveEmptyEntries);

foreach(var s in split){
   var split2 = s.Split(new string[] {"**"}, StringSplitOptions.RemoveEmptyEntries);
}

#4


1  

This will give you an IEnumerable<IEnumerable<string>> containing the values you want.

这将为您提供包含所需值的IEnumerable >。

string[] split1 = new[] { "^__^" };
string[] split2 = new[] { "**" };
StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
var vals = s.Split(split1,op).Select(p => p.Split(split2,op).Cast<string>());

You can also throw some ToList()'s in there if you wanted List<List<string>> or ToArray()'s for a jagged array

如果你想要一个锯齿状数组的List >或ToArray(),你也可以在那里抛出一些ToList()

#5


0  

How about one of these:

其中一个怎么样:

string.Join("**",yourString.Split("^__")).Split("**")
yourString.Replace("^__","**").Split("**")

You will have to check the last item, in your example it would be an empty string.

你必须检查最后一项,在你的例子中它将是一个空字符串。

#6


0  

As already stated, you can pass in a string array to the Split method. Here's how the code might look based on your recent edits to the quesiton:

如前所述,您可以将字符串数组传递给Split方法。以下是基于您最近对问题的编辑,代码的外观:

string x = "aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^";
string[] y =  x.Split(new string[]{"^__^"},StringSplitOptions.None);
string[] z = y[0].Split(new string[]{"**"},StringSplitOptions.None);

#7


0  

The VB.NET Version that works.

有效的VB.NET版本。

Module Module1

Sub Main()
    Dim s As String = "aa**aa**bb**dd^__^a2a**a2a**b2b**dd^__^"
    Dim delimit As Char() = New Char() {"*"c, "^"c, "_"c}

    Dim split As String() = s.Split(delimit, StringSplitOptions.RemoveEmptyEntries)
    Dim c As Integer = 0
    For Each t As String In split
        c += 1
        Console.WriteLine(String.Format("{0}: {1}", c, t))
    Next

End Sub