Home >Backend Development >C++ >How to Split Strings by Substrings in C#?

How to Split Strings by Substrings in C#?

Linda Hamilton
Linda HamiltonOriginal
2025-01-23 19:36:10535browse

How to Split Strings by Substrings in C#?

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.

The

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn