removing suffiex or prefix from sting
조회 수: 43 (최근 30일)
이전 댓글 표시
Hi there
i want code which delete the suffix or prefix in string please?
댓글 수: 0
채택된 답변
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
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!