Home >Backend Development >C++ >How to Extract Text Within Parentheses in C#?
C# Multiple methods to extract text within brackets
There are many ways to extract the text within brackets from a string, regular expressions are a commonly used method. However, if you don't want to use regular expressions, you can use the simpler Split
method.
For example, for the string "User name (sales)", you can use the following C# code snippet to extract the text within the brackets:
<code class="language-csharp">string input = "User name (sales)"; string output = input.Split('(', ')')[1]; // 输出:sales</code>
In this code, the Split
method uses left and right brackets as delimiters. In the returned substring array, the element at index 1 is the text "sales" within the brackets. This method is simple and effective, and can extract the text inside the brackets without using regular expressions.
The above is the detailed content of How to Extract Text Within Parentheses in C#?. For more information, please follow other related articles on the PHP Chinese website!