Remove parenthesis and the contents inside from a string

조회 수: 55 (최근 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 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