How to break this line in C #?

I'm really not used to the split string method in C #, and I was wondering how more than one char function doesn't break there?

and my attempt to try to split this line below with regex just ended in disappointment. can anyone help me?

Basically, I want to split the line below to

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

in

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

and then later in

 aa aa bb dd a2a a2a b2b dd 

thanks!

+4
source share
7 answers

You can split using 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" 
+10
source

You are looking for this overload :

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

I never understood why there is no String.Split(params string[] separators) .
However, you can write it as an extension method:

 public static string[] Split(this string str, params string[] separators) { return str.Split(separators, StringSplitOptions.None); } 
+7
source
 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); } 
+2
source

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

 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 ToList() if you want List<List<string>> or ToArray() for the jagged array

+1
source

How about one of them:

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

You will need to check the last element, in your example it will be an empty string.

0
source

As already mentioned, you can pass an array of strings to the Split method. Here's what the code based on the latest changes to quesiton looks like:

 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); 
0
source

The version of VB.NET is working.

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 
0
source

Source: https://habr.com/ru/post/1307033/


All Articles