Extracting 2 Far right characters

1 次查看(过去 30 天)
Hi,
How could I extract the 2 far right characters from cell string?
tt= {'United Kingdom TR';'United Kingdom SR';'United Kingdom WR';'Worldwide TC'};
Below is very inefficient.
splitStr = regexp( tt, ' ','split');
for runi= 1: length( tt)
ttxt = splitStr{runi};
peril(runi) = string( ttxt(end));
end
peril =
1×4 string array
"TR" "SR" "WR" "TC"

回答(2 个)

Les Beckham
Les Beckham 2022-5-6
One approach that generates a cell array:
tt= {'United Kingdom TR';'United Kingdom SR';'United Kingdom WR';'Worldwide TC'};
c = cellfun(@(s)s(end-1:end), tt, 'UniformOutput', false)
c = 4×1 cell array
{'TR'} {'SR'} {'WR'} {'TC'}
If you prefer, you can convert this result to a string array like this
string(c)
ans = 4×1 string array
"TR" "SR" "WR" "TC"

Stephen23
Stephen23 2022-5-6
tt = {'United Kingdom TR';'United Kingdom SR';'United Kingdom WR';'Worldwide TC'};
pe = regexp(tt,'\w\w$','match','once')
pe = 4×1 cell array
{'TR'} {'SR'} {'WR'} {'TC'}

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by