Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 21 Jan. 2014
Bearbeitet: Andrei Bobrov am 21 Jan. 2014

2 Stimmen

a = 'bbc.ed.123.f.txt';
regexprep(a,{'\.','txt'},{'','.txt'})
or
[strrep(a(1:end-4),'.',''),'.txt']

1 Kommentar

Roger
Roger am 22 Jan. 2014
if i also want to replace '-' with '_',and ' 'with '',i should do like this
a=[strrep(a(1:end-4),'.',''),'.txt'];
a=[strrep(a(1:end-4),'-','_'),'.txt'];
a=[strrep(a(1:end-4),' ',''),'.txt'];
is there easy way?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

ES
ES am 21 Jan. 2014

2 Stimmen

you have to handle the last dot(before file extension) and the other dots separately.
str='bbc.ed.123.f.txt';
idx=strfind(str,'.');
new_str=[strrep(str(1:idx(end)-1),'.',''),str(idx(end):end)];%idx(end) is the position of the last dot!

Kategorien

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

Tags

Gefragt:

am 21 Jan. 2014

Kommentiert:

am 22 Jan. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by