removing suffiex or prefix from sting

조회 수: 55 (최근 30일)
Ebtesam Almansor
Ebtesam Almansor 2016년 10월 4일
댓글: Thomas Pajenkamp 2019년 7월 19일
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