Home > Backend Development > C++ > How to Split a String by the '#' Character in C#?

How to Split a String by the '#' Character in C#?

Mary-Kate Olsen
Release: 2025-01-13 22:29:44
Original
525 people have browsed it

How to Split a String by the '#' Character in C#?

Use '#' character to split string in C#

Splitting a string based on a specific character such as '#' is a common task in C#. Suppose there is a string "Hello#World#Test", to extract "Hello", "World" and "Test" into separate strings, you can use the following method:

The

Split() method can split a string into an array of substrings based on the provided character or string. For example:

<code class="language-csharp">string[] splitString = "Hello#World#Test".Split('#');</code>
Copy after login

This operation produces an array named splitString containing three elements:

  • splitString[0] = "Hello"
  • splitString[1] = "World"
  • splitString[2] = "Test"
The

'#' character is used as a split delimiter, ensuring that the string is split every time this character appears. This makes it easy to extract individual words and store them in separate variables:

<code class="language-csharp">string string1 = splitString[0];
string string2 = splitString[1];
string string3 = splitString[2];</code>
Copy after login

Now, the three strings "Hello", "World" and "Test" are stored in string1, string2 and string3 respectively, allowing easy access to the extracted data.

The above is the detailed content of How to Split a String by the '#' Character 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