


KEEPBIG5 Keep big5 character in a string; gets rid of non-big5 characters
Usage: OUT = KEEPBIG5(INPUT)
INPUT = false two-byte Chinese big5 string
(See the example in the self-demo.)
OUT = true two-byte Chinese big5 string
If the input is a true two-byte Chinese big5 string, you can simply
get rid of non-big characters by the one-liner:
str(find(str < hex2dec('a440'))) = [];
Roger Jang, 981107

0001 function out = keepbig5(str) 0002 % KEEPBIG5 Keep big5 character in a string; gets rid of non-big5 characters 0003 % Usage: OUT = KEEPBIG5(INPUT) 0004 % INPUT = false two-byte Chinese big5 string 0005 % (See the example in the self-demo.) 0006 % OUT = true two-byte Chinese big5 string 0007 % 0008 % If the input is a true two-byte Chinese big5 string, you can simply 0009 % get rid of non-big characters by the one-liner: 0010 % str(find(str < hex2dec('a440'))) = []; 0011 % 0012 % Roger Jang, 981107 0013 0014 if nargin == 0, 0015 str =[186 244 184 244 164 87 44 45 124 170 186 170 218 190 70 56]; 0016 str = char(str); 0017 fprintf('Original string = %s\n', str); 0018 fprintf('Original length = %g\n', length(str)); 0019 big5 = keepbig5(str); 0020 fprintf('New string = %s\n', big5); 0021 fprintf('New length = %g\n', length(big5)); 0022 end 0023 0024 len = length(str); 0025 del_index = []; 0026 i = 1; 0027 while (i < len) 0028 if mod(i, 100) == 0, 0029 fprintf('%g/%g\n', i, len); 0030 end 0031 double_byte = str(i:i+1); 0032 if isbig5(double_byte), 0033 i = i + 2; 0034 else 0035 del_index = [del_index, i]; 0036 i = i + 1; 0037 end 0038 end 0039 0040 out = str; 0041 % Get rid of the non-big5 characters 0042 out(del_index) = []; 0043 % Get rid of the last byte if the length is odd 0044 if mod(length(out), 2) ~= 0, 0045 out(end) = []; 0046 end 0047 0048 % Recombine to have true double-byte characters 0049 out = char(out(1:2:end)*256+out(2:2:end));