Home > Backend Development > C++ > How to Split a String by Another String in C#?

How to Split a String by Another String in C#?

Patricia Arquette
Release: 2025-01-23 19:27:10
Original
494 people have browsed it

How to Split a String by Another String in C#?

Split a string using another string in C#

The

Split() 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:

  • THE
  • QUICK
  • BROWN
  • FOX

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>
Copy after login
The

StringSplitOptions.None parameter specifies that no special splitting options should be applied.

Result:

The

splitData array will contain the following substrings:

  • THE
  • QUICK
  • BROWN
  • FOX

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template