I have a question about a code. I have a cell array with strings and I would like to replace the last 5 characters with another for every cell array. I tried strrep but no use. For example I have an array: A={'MATHIEW','NIKOLIEW','SAMIEW','SAMOURAI'}. I would like to replace ONLY in the strings that their last 3 characters have the elen\ments: IEW, with the elements 'YAU'. I mean that I want my final array to be: B={'MATHYAU','NIKOLYAU','SAMYAU','SAMOURAI'}.
Kshitij Singh answered .
2025-11-20
Try the function endsWith in a loop to check if the string ends with certain characters
B={'MATHIEW','NIKOLIEW','SAMIEW','SAMOURAI'}
B = 1×4 cell array
{'MATHIEW'} {'NIKOLIEW'} {'SAMIEW'} {'SAMOURAI'}
for k = 1 : numel(B)
if endsWith(B{k}, 'IEW', 'IgnoreCase', true)
B{k}(end-2 : end) = 'YAU';
end
end
B % Show in command window
B = 1×4 cell array
{'MATHYAU'} {'NIKOLYAU'} {'SAMYAU'} {'SAMOURAI'}