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:
TheSplit()
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>
This operation produces an array named splitString
containing three elements:
splitString[0]
= "Hello"splitString[1]
= "World"splitString[2]
= "Test"'#' 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>
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!