Remove simultaneously several substrings in a string

1 visualización (últimos 30 días)
Sim
Sim el 16 de Dic. de 2021
Comentada: Mathieu NOE el 16 de Dic. de 2021
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 "

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 16 de Dic. de 2021
hello
try this
A = "159 (51,1%) 13 (4,2%) 139 (44,7%)";
B = split(A,' ');
B(contains(B,'(')) = [];
B = join(B,' ');
  3 comentarios
Sim
Sim el 16 de Dic. de 2021
Editada: Sim el 16 de Dic. de 2021
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 el 16 de Dic. de 2021
My pleasure ,
yes , sure , there are probably even other possibilities

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by