Split string using substring in C#
While the Split()
method is typically used to split strings based on a single character, it is also possible to split strings using longer substrings.
Solution:
To split a string by a specific substring, use the string array overload of the Split()
method. Here's an example:
<code class="language-csharp">string data = "THExxQUICKxxBROWNxxFOX"; string splitter = "xx"; string[] splitResult = data.Split(new string[] { splitter }, StringSplitOptions.None);</code>
This code will return an array containing the following values:
<code>["THE", "QUICK", "BROWN", "FOX"]</code>
Instructions:
In the string array overload of Split()
, the first parameter represents the substring array to be split. In this example, we create an array containing the delimiter string and pass it as a parameter.
StringSplitOptions.None
parameter specifies that splitting should be done based on the entire delimiter substring, not on any of its characters.
The above is the detailed content of How to Split Strings by Substrings in C#?. For more information, please follow other related articles on the PHP Chinese website!