Is there a way to detect string/letter not compatible with windows-1252 encoding?

5 次查看(过去 30 天)
Hi,
Is there a way to detect string/letter not compatible with windows-1252 encoding And then remove them?

回答(1 个)

Walter Roberson
Walter Roberson 2022-11-29
Yes. When you unicode2native a character that has no counterpart in the destination character set, then binary 26 is substituted.
S = char(['How now?', 2000:2029, ' brown cow'])
S = 'How now?ߐߑߒߓߔߕߖߗߘߙߚߛߜߝߞߟߠߡߢߣߤߥߦߧߨߩߪ߫߬߭ brown cow'
B = unicode2native(S, 'windows-1252')
B = 1×48
72 111 119 32 110 111 119 63 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
B(B == 26) = []
B = 1×18
72 111 119 32 110 111 119 63 32 98 114 111 119 110 32 99 111 119
Sback = native2unicode(B, 'windows-1252')
Sback = 'How now? brown cow'
  1 个评论
Pete sherer
Pete sherer 2022-11-30
编辑:Pete sherer 2022-11-30
This resolves the problem.
My string is either cellstr or string type. Is there a way to bypass using char via a for loop?
City= {'Serra';'Anápolis';'CONCEIÇÃO DE FEIRA';'CONCEIÇÃO DO JACUÃPE'; 'Test ߙߚߛߜߝߞߟߠߡߢߣߤߥ'};
for runi = 1: length(City)
ori = unicode2native( char( City{ runi}), 'windows-1252');
if any(ori==26), disp(['runi=' num2str(runi)]); ori(ori==26)=[]; end;
tnew(runi) = cellstr(native2unicode( ori, 'windows-1252'));
end
Also if I want to check compatibility with UTF-8, do I still check it against 26?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by