Home > Backend Development > C++ > How to Split Strings with Multi-Character Delimiters in C#?

How to Split Strings with Multi-Character Delimiters in C#?

Mary-Kate Olsen
Release: 2025-01-13 12:31:52
Original
206 people have browsed it

How to Split Strings with Multi-Character Delimiters in C#?

Use multi-character delimiter to split string in C#

When splitting a string using the Split method in C#, the default behavior is to use a single character delimiter. However, in some cases you may need to use a delimiter that consists of multiple characters, such as a word.

To do this, you can specify a delimiter string when calling the Split method. The following example demonstrates how to split the string "This is a sentence" using the delimiter "is":

<code class="language-csharp">string source = "This is a sentence";
string[] stringSeparators = new string[] { "is" };
string[] result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "" : s);
}</code>
Copy after login

This code will produce the following output:

<code>'This ' 'a sentence' ' '</code>
Copy after login
The

StringSplitOptions.None argument indicates that the split should not remove empty elements from the resulting array. If you want to remove empty elements, you can specify StringSplitOptions.RemoveEmptyEntries instead.

Further reading:

The above is the detailed content of How to Split Strings with Multi-Character Delimiters 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