removing suffiex or prefix from sting

51 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by