Home >Backend Development >C++ >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!