Question:
Getting what is contained within the brackets (round brackets) from a given string can be a challenge. Consider the string "username(sales)" and the goal is to extract the text from the brackets. How can I achieve this without relying on regular expressions?
Answer:
To extract the text inside brackets without using regular expressions, a straightforward approach involves splitting the input string:
<code>string input = "用户名 (销售)"; string[] parts = input.Split('(', ')'); string output = parts[1];</code>
This split operation creates an array where each element represents a string segment separated by bracket characters. The second element of the array (parts[1]) contains the text within the brackets, which can then be assigned to the output variable.
The above is the detailed content of How Can I Extract Text from Parentheses Without Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!