10-3 �r�ꪺ����B�M��B�N���B���ѻP���X

strcmp «ü¥O¥Î©ó¤ñ¸û¦r¦ê¤º®eªº²§¦P¡A¨Ò¦p¡G

Example 1: 10-¦r¤¸»P¦r¦ê/string301.mstr1 = 'today'; str2 = 'tomorrow'; str3 = 'today'; out1 = strcmp(str1, str2) % ¤ñ¸û¨â¦r¦ê str1 ©M str2 out2 = strcmp(str1, str3) % ¤ñ¸û¨â¦r¦ê str1 ©M str3 out1 = 0 out2 = 1

¥Ñ¤W¨Ò±oª¾¡Aout1 ªº­È¬O 0¡Aªí¥Ü str1 ©M str2 ¤º®e¤£¬Ûµ¥¡Fout2 ªº­È¬O 1¡Aªí¥Ü str1 ©M str3 ¤º®e¬Ûµ¥¡C

Hint
¦bC »y¨¥¤¤¡Astrcmp «ü¥O¤]¥Î¨Ó§@¦r¦ê¤ñ¸û¡A¦ý¦b¨â¦r¦ê¬Ûµ¥®É¡A¦^¶Ç­È¬O 0¡A³o©M MATLAB ªº strcmp «ü¥O­è¦n¬Û¤Ï¡C½Ð¯S§O¤p¤ß¡I

strncmp «ü¥O¥Î©ó¤ñ¸û¦r¦êªº«e n ­Ó¦r¤¸¡A¨Ò¦p¡G

Example 2: 10-¦r¤¸»P¦r¦ê/string302.mstr1 = 'today'; str2 = 'tomorrow'; strncmp(str1, str2, 2) % ¤ñ¸û str1 ¤Î str2 ¨â¦r¦êªº«e 2 ­Ó¦r¤¸ ans = 1

strcmp ¤Î strncmp «ü¥O¥ç¥i¥Î©ó¦r¦ê²§½è°}¦C¡A¨Ò¦p¡G

Example 3: 10-¦r¤¸»P¦r¦ê/string303.mA = {'¥x¥_', '¥x¤¤', '°ª¶¯'}; % «Ø¥ß¦r¦ê²§½è°}¦C A B = {'¥x¥_', '¥x«n', 'ªá½¬'}; % «Ø¥ß¦r¦ê²§½è°}¦C B out1 = strcmp(A, B) % ¤ñ¸û¦r¦ê²§½è°}¦C A »P B ªº¨C­Ó¤¸¯À¬O§_¬Û¦P out2 = strncmp(A, B, 1) % ¤ñ¸û¦r¦ê²§½è°}¦C A »P B ªº¨C­Ó¤¸¯Àªºªº²Ä¤@­Ó¦r¤¸¬O§_¬Û¦P out1 = 1 0 0 out2 = 1 1 0

findstr «ü¥O¥i¥H´M§ä¦b¬Y¤@­Óªø¦r¦ê¤¤ªº¤l¦r¦ê¡]Substrings¡^¡A¨Ã¶Ç¦^¨ä°_©l¦ì¸m¡A¨Ò¦p¡G

Example 4: 10-¦r¤¸»P¦r¦ê/string304.mstring = '§Ú³Ì³ßÅwªº¹B°Ê¬Oºô²y'; pattern = 'ºô²y'; position = findstr(string, pattern) position = 9

strrep «ü¥O¥Î©ó¦r¦ê´M§ä¤Î¥N´«¡A¨Ò¦p¡G

Example 5: 10-¦r¤¸»P¦r¦ê/string305.mstring = '§Ú³Ì³ßÅwªº¹B°Ê¬Oºô²y'; pattern = 'ºô²y'; pattern2 = '½¼²y'; newString = strrep(string, pattern, pattern2) newString = §Ú³Ì³ßÅwªº¹B°Ê¬O½¼²y

strtok «ü¥O¥i®Ú¾Ú¤@µ¹©wªº¤À¬É¦r¤¸¡]Delimiting Characters¡^¡A±N¤@¦r¦ê©î¸Ñ¦¨¼Æ­Ó¦r¦ê¡A¹w³]ªº¤À¬É¦r¤¸¬°ªÅ¥Õ¦r¤¸¡A¨Ò¦p¡G

Example 6: 10-¦r¤¸»P¦r¦ê/string306.minput_string = 'ee cs econ stat me'; remainder = input_string; parsed = ''; % «Ø¥ß¤@ªÅ¦r¤¸°}¦C while (any(remainder)) [chopped, remainder] = strtok(remainder); parsed = strvcat(parsed, chopped); end parsed parsed = ee cs econ stat me

¦b¤W¨Ò¤¤¡Astrvcat «ü¥Oªº§@¥Î¬O±N©î¸Ñ¤U¨Óªº¦r¦ê¤º®e¡]§YÀx¦s¦b¦r¦êÅÜ¼Æ chopped ¤¤¡^ ¥[¨ì¤Gºû¦r¤¸°}¦C parsed ¤¤¡C

¤@¯ë¦Ó¨¥¡A­Y¬O¦b MATLAB «ü¥Oµøµ¡©Î¬Oµ{¦¡½Xª½±µ«ü©w§t¦³¤j¤­½Xªº¦r¦ê¡AMATLAB 7.x ·|ª½±µ¨Ï¥Î unicode ¨ÓÀx¦s¤º½X¡C¦ý¬O¦pªGª½±µ±qÀÉ®×Ū¨ú¤å¦r¡A«h MATLAB ·|¹ï¨C¤@­Ó¤¤¤å¤å¦r©î¦¨¨â­Ó byte ¨ÓŪ¤J¡A³y¦¨¡u¥Yºl¡v¡CÁ|¨Ò¨Ó»¡¡A¦b¥»³¹½d¨Òµ{¦¡¥Ø¿ý¤º¡A¦³¤@Àɮ׬°¡ubig5.txt¡v¡A¨ä¤º®e¬°¡u§Ú¬ORoger¡v¡A§Ú­Ì¥i¥Î¤U¦Cµ{¦¡±N¦¹Àɮפº®eŪ¦Ü¤@­Ó¦r¦êÅܼơG

Example 7: 10-¦r¤¸»P¦r¦ê/string307.mfid = fopen('big5.txt'); line = fgetl(fid) % Ū¨ú¤@¦CÀɮפº®e¨Ã¦L¥X fclose(fid); leng=length(line) % Åã¥Ü¦r¦êÅܼƪø«× line = §Ú¬ORoger leng = 7

¡]¦³ÃöŪ¨ú¦¹¤å¦rÀɤ§¤èªk¡A¥i¸Ô¨£¥»®Ñ²Ä¤Q¤K³¹¡uÀɮ׿é¥X¤Î¿é¤J¡v¡C¡^¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì±N¦r¦êÅÜ¼Æ line ¦L¥X¡Aµoı«e­±¨â­Ó¤¤¤å¦r¤w¸gÅܦ¨¶Ã½X¡A¦Ó¥Bline ªºªø«×¬O 9¡A¦Ó¤£¬O 7 ¡A±ý¸Ô¬d­ì¦]¡A§Ú­Ì¥i¥Î double «ü¥O±N¤§Âন ASCII ½X¡A´N¯à¤@¥Ø¤FµM¡G

Example 8: 10-¦r¤¸»P¦r¦ê/string308.mfid = fopen('big5.txt'); line = fgetl(fid); % Ū¨ú¤@¦CÀɮפº®e fclose(fid); double(line) % Åã¥Ü¦r¦ê¤º½X ans = Columns 1 through 5 25105 26159 82 111 103 Columns 6 through 7 101 114

­ì¨Ó MATLAB ¦bŪ¤J¤¤¤å¦r¦ê®É¡A±N¨C¤@­Ó¤¤¤å¦rªº 2-byte ¤À¶}¨ÓŪ¡A³y¦¨­ì¥ýªº¨â­Ó¤¤¤å¦rÅܦ¨¥|­Ó¦r¦ê¤¸¯À¡C¡]­Y¨Ï¥Î MATLAB 6.x¡AÁöµM¥~Æ[¬Ý¤£¥X¨Ó¡A¦ý¤º³¡Àx¦s¤è¦¡¤´¬O±N¤@­Ó¤¤¤å¦r¬Ý¦¨¨â­Ó³æ­ì¨ÓÀx¦s¡C¡^±ý¸Ñ¨M¦¹°ÝÃD¡A¦b MATLAB 7.x ¥i¥Î native2unicode «ü¥O¡A¨Ó±N¤¤¤åªº 2-byte ¡uµ²¦X¡v¦b¤@°_¡A¨Ò¦p¡G

Example 9: 10-¦r¤¸»P¦r¦ê/string309.mfid = fopen('big5.txt'); line = fgetl(fid); % Ū¨ú¤@¦CÀɮפº®e fclose(fid); line2 = native2unicode(line, 'big5') % ¨Ï¥Î native2unicode ±N³Q©î¶}ªº¤¤¤å¦rµ²¦X¦b¤@°_ leng = length(line2) % Åã¥Ü¦r¦êªø«× line2 = §Ú¬ORoger leng = 7

¦]¦¹¡A¦pªG±zªºµ{¦¡»Ý­n±`±`Ū¨ú¤¤¤åÀɮס]©Î¬O¨ä¥¦ double bytes ªº¤å¦r¡^¡Anative2unicode«ü¥O±N¬O§Aªº³Ì¨Î¥ë¦ñ¡I

­Y­n¦b MATLAB 5.3 ©Î 6.x °õ¦æ¤W­z½d¨Ò¡A½Ð±N native2unicode(line, 'big5') §ï¦¨ xlate(line) ¡A§Y¥i±o¨ì¦P¼Ëªºµ²ªG¡C

¡uxlate¡v«ü¥O¬Oµ§ªÌ¦V¬ü°êÁ`¤½¥q¬d¸ß¦p¦ó¸Ñ¨M¤W­z°ÝÃD©Ò±o¨ìªº¸Ñµª¡A¦b MATLAB 5.3 ª©«á¤~¦³¤ä´©¡A¦ý¦b MATLAB 7.x ¤w¸g¥\¦¨¨­°h¡C¦¹«ü¥O¬O ¡uundocumented¡v¡A¦]¦¹¦b MATLAB ½u¤W¤ä´©©Î¯È¥»¤â¥U³£¥¼´£¨ì¡C


MATLABµ{¦¡³]­p¡G¤Jªù½g