Remove parenthesis and the contents inside from a string

50 ビュー (過去 30 日間)
Yuzhen Lu
Yuzhen Lu 2021 年 1 月 7 日
コメント済み: Stephen23 2023 年 3 月 9 日
Is there a neat way to remove a parenthesis and the contents inside from a string. For example the string
A = 'abc (ABC)'
% how to extract 'abc' from A, get rid of ' (ABC)' including the leading whitespace?
One cumbersome solution is:
temp = strsplit(A,'(');
B = strtrim(temp(1));

採用された回答

Stephen23
Stephen23 2021 年 1 月 7 日
A = 'abc (ABC)';
B = regexp(A,'^\w+','once','match')
B = 'abc'
  5 件のコメント
Ivan Mich
Ivan Mich 2023 年 3 月 9 日
編集済み: Ivan Mich 2023 年 3 月 9 日
Excuse me I have a question.. how could you do the inverse of this??
I mean to extract the only the characters that exist in brackets without the others.
for example:
input : A = 'abc (ABC)';
output B = 'ABC'
could you please help me?
Stephen23
Stephen23 2023 年 3 月 9 日
"I mean to extract the only the characters that exist in brackets without the others."
A = 'abc (ABC)';
B = regexprep(A,{'.*\(','\).*'},'')
B = 'ABC'

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 1 月 7 日
A = 'abc (ABC)' ;
idx = strfind(A,' (') ;
iwant = A(1:idx-1)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by