Remove simultaneously several substrings in a string

7 ビュー (過去 30 日間)
Sim
Sim 2021 年 12 月 16 日
コメント済み: Mathieu NOE 2021 年 12 月 16 日
Hi, I have a string
A = "159 (51,1%) 13 (4,2%) 139 (44,7%)";
and I would need to remove all the numbers among brackets (and brackets included!). My goal is to get this new string:
A = "159 13 139";
Any idea ?
I tried this, but it is not working:
>> strtok(A,'()')
ans =
"159 "

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 12 月 16 日
hello
try this
A = "159 (51,1%) 13 (4,2%) 139 (44,7%)";
B = split(A,' ');
B(contains(B,'(')) = [];
B = join(B,' ');
  3 件のコメント
Sim
Sim 2021 年 12 月 16 日
編集済み: Sim 2021 年 12 月 16 日
Many thanks, it work very well!
...Just for fun and curiosity, I also tried textscan with your solution..
B = textscan(A,'%s','Delimiter',' ')';
B{:}(contains(B{:},'(')) = []
B = join(B{:},' ')
which gives:
B =
1×1 cell array
{'159 13 139'}
Mathieu NOE
Mathieu NOE 2021 年 12 月 16 日
My pleasure ,
yes , sure , there are probably even other possibilities

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by