Home >Backend Development >C++ >How to Split a String with a String Delimiter in C#?

How to Split a String with a String Delimiter in C#?

DDD
DDDOriginal
2025-01-11 07:05:421044browse

How to Split a String with a String Delimiter in C#?

C# string splitting: use string delimiter

This problem is often encountered when you need to split a string into multiple substrings based on a specified delimiter. This article explores how to split a string using the specific string "is Marco and" as a separator, rather than a single character.

The

solution highlights the Split() approach using delimited arrays. The following code snippet demonstrates this approach:

<code class="language-csharp">string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);</code>

Where, str is the original string to be split. The StringSplitOptions.None parameter ensures that splitting is based on an exact match of the delimiter string.

However, if your delimiter is a single character (for example, a comma), you can simplify the code to:

<code class="language-csharp">string[] tokens = str.Split(',');</code>

For single character delimiters, this method is more concise and efficient.

The above is the detailed content of How to Split a String with a String Delimiter 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