Home >Backend Development >C++ >How Can I Extract Text Enclosed in Parentheses Using String Manipulation?

How Can I Extract Text Enclosed in Parentheses Using String Manipulation?

Linda Hamilton
Linda HamiltonOriginal
2025-01-18 03:11:08751browse

How Can I Extract Text Enclosed in Parentheses Using String Manipulation?

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!

Statement:
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