removing suffiex or prefix from sting

56 Ansichten (letzte 30 Tage)
Ebtesam Almansor
Ebtesam Almansor am 4 Okt. 2016
Kommentiert: Thomas Pajenkamp am 19 Jul. 2019
Hi there
i want code which delete the suffix or prefix in string please?

Akzeptierte Antwort

KSSV
KSSV am 4 Okt. 2016
clc; clear all ;
str = 'unbecomingly';
prefix = 'un'; % The prefix to remove
suffix = 'ly'; % The suffix to remove
%
str = strrep(str,prefix,'') ;
str = strrep(str,suffix,'') ;
  1 Kommentar
Thomas Pajenkamp
Thomas Pajenkamp am 19 Jul. 2019
For people stumbling upon this thread for an answer: This solution also removes parts in between the string if they happen to match the given prefix or suffix.
E.g.:
strrep('ABC01ABC123', 'ABC', '')
becomes
'01123'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Elias Gule
Elias Gule am 4 Okt. 2016
try this:
str = 'unbecomingly';
prefix = 'un'; % The prefix to remove
suffix = 'ly'; % The suffix to remove
regex = {['^' prefix],[suffix '$']}; % the regular expressions for prefix & suffix
replacements = {'',''}; % Replacement strings
newstr = regexprep(str,regex,replacements); % The new string with suffix or prefix or both replaced by corresponding replacement string.

Ebtesam Almansor
Ebtesam Almansor am 6 Okt. 2016
thank you very much

Kategorien

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by