Extract text in brackets (parentheses)
Extracting bracketed text from a string is a common task in programming. For example, the string "User name (sales)", how to extract "sales" using string manipulation techniques?
A straightforward approach is to use the Split method to retrieve a substring from the source string. This method splits a string into an array of substrings based on a delimiter. Since the text we are looking for is enclosed in parentheses, we can use a parenthesis as a delimiter.
<code>string input = "User name (sales)"; string separator = "(" + ")"; string output = input.Split(separator, StringSplitOptions.RemoveEmptyEntries)[1];</code>
In this example, the source string is split into ["User name ", "sales"] and then the second substring is retrieved using the index.
The above is the detailed content of How Can I Extract Text Enclosed in Parentheses Using String Manipulation?. For more information, please follow other related articles on the PHP Chinese website!