removing suffiex or prefix from sting
43 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ebtesam Almansor
le 4 Oct 2016
Commenté : Thomas Pajenkamp
le 19 Juil 2019
Hi there
i want code which delete the suffix or prefix in string please?
0 commentaires
Réponse acceptée
KSSV
le 4 Oct 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 commentaire
Thomas Pajenkamp
le 19 Juil 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'
Plus de réponses (2)
Elias Gule
le 4 Oct 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!