removing suffiex or prefix from sting

40 次查看(过去 30 天)
Hi there
i want code which delete the suffix or prefix in string please?

采纳的回答

KSSV
KSSV 2016-10-4
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 个评论
Thomas Pajenkamp
Thomas Pajenkamp 2019-7-19
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'

请先登录,再进行评论。

更多回答(2 个)

Elias Gule
Elias Gule 2016-10-4
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 2016-10-6
thank you very much

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by