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
As of .NET Core 2.0, there is an override that takes a string.
So now you can do "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_
There's an overload of String.Split for this:
"THExxQUICKxxBROWNxxFOX".Split(new [] {"xx"}, StringSplitOptions.None);
There is an overload of Split that takes strings.
"THExxQUICKxxBROWNxxFOX".Split(new [] { "xx" }, StringSplitOptions.None);
You can use either of these StringSplitOptions
So if the string is "THExxQUICKxxxxBROWNxxFOX", StringSplitOptions.None
will return an empty entry in the array for the "xxxx" part while StringSplitOptions.RemoveEmptyEntries
will not.
This is also easy:
string data = "THExxQUICKxxBROWNxxFOX";
string[] arr = data.Split("xx".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
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);
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)!