How to replace only one instance in a certain string

41 Ansichten (letzte 30 Tage)
Michael Boyle
Michael Boyle am 18 Okt. 2021
Kommentiert: Stephen23 am 18 Okt. 2021
I am trying to use strrep() to replace the first 'e' with nothing '' and leave the second one alone in the following string:
x = 'e-8.5e-5';
y = strrep(x,'e','');
desired_final_ans = '-8.5e-5';
Is there a way to skip instances in strrep()?

Akzeptierte Antwort

the cyclist
the cyclist am 18 Okt. 2021
If you are certain that the first e is the first character of the string, then this will do it:
x = 'e-8.5e-5'
x = 'e-8.5e-5'
y = regexprep(x,'^e','')
y = '-8.5e-5'
The '^' indicates that the regular expression has to start the string.

Weitere Antworten (1)

Image Analyst
Image Analyst am 18 Okt. 2021
Here's one way:
eLocations = strfind(x, 'e')
x(eLocations(1)) = [] % Replace first e with null (in other words, delete it).

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by