Split a string using another string in C#
TheSplit()
method in C# is typically used to split a string based on a single character. However, in some cases you may need to split a string using another string as a delimiter. This article demonstrates how to achieve this using the string array overload of the Split()
method.
Question:
Given a string containing multiple delimiters, you need to split the string into an array of substrings, each substring separated by a delimiter.
Example:
Split the string "THExxQUICKxxBROWNxxFOX" using the delimiter "xx" to get the following array:
Solution:
To split a string using another string, you can use the string array overload of the Split()
method. This overload takes an array of strings as a delimiter argument, allowing you to specify multiple delimiters if necessary.
The following code demonstrates how to split a string using the delimiter "xx":
<code class="language-csharp">string data = "THExxQUICKxxBROWNxxFOX"; string[] splitData = data.Split(new string[] { "xx" }, StringSplitOptions.None);</code>
StringSplitOptions.None
parameter specifies that no special splitting options should be applied.
Result:
ThesplitData
array will contain the following substrings:
The above is the detailed content of How to Split a String by Another String in C#?. For more information, please follow other related articles on the PHP Chinese website!