How do I replace/manipulate characters in a string? Specifically integers in a string...

If I have a string like axiom = 'F18-F4+F'; I need to manipulate the string so that it reads as follows: F------------------F++++F
Basically I need for the numbers in the string to behave as integer multipliers on the + and - sign.
I have tried many combinations and approaches using regex, str2Double, isstrprop(axiom, 'alpha'); Anyone have any advice on this kind of string manipulation? Thanks in advance!

2 comentarios

I've no klew what the "I need" expression is intended to mean?
Give us the rule for how to generate the output from the input.
mars
mars el 29 de En. de 2017
Editada: mars el 29 de En. de 2017
In the expression axiom = 'F18-F4+F'; 18 and 4 are multipliers on the - and + signs. I need to manipulate the expression so that it reads F------------------F++++F. The rules are: 1) Loop over the string 2) if a position in the string is an integer it should act as a multiplier on the following plus or minus sign. Does that help? I apologize for the lack of clarity. I made edits to my post above as well. Thanks @dpb!

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 29 de En. de 2017
Editada: Stephen23 el 29 de En. de 2017
One easy way with regexprep:
>> str = 'F18-F4+F';
>> regexprep(str,'(\d+)([+-])','${repmat($2,1,str2double($1))}')
ans =
F------------------F++++F

1 comentario

You are a boss! Thanks so much! I am new to matlab and taking a graduate course and still trying to figure out the easiest things! Thanks again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 29 de En. de 2017

Comentada:

el 29 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by