Remove parenthesis and the contents inside from a string

54 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by