某公司面试是在的解决方法

上周某公司笔试时遇到的题目,题目描述如下:

编程题
编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。

 这道题目的关键点有两个:

1、汉字按照2字节,英文字母按照1字节进行截取(需要找到对应的编码格式)

2、如何判断哪个是汉字,哪个是英文字母(需要找到区分汉字与字母的方法)

 

关于编码格式(参考文章),哪种编码能符合题目的要求呢,请看下面(参考文章):

Java代码  收藏代码
  1. import java.io.UnsupportedEncodingException;  
  2.   
  3. public class EncodeTest {  
  4.     /** 
  5.      * 打印字符串在指定编码下的字节数和编码名称到控制台 
  6.      *  
  7.      * @param s 
  8.      *            字符串 
  9.      * @param encodingName 
  10.      *            编码格式 
  11.      */  
  12.     public static void printByteLength(String s, String encodingName) {  
  13.         System.out.print("字节数:");  
  14.         try {  
  15.             System.out.print(s.getBytes(encodingName).length);  
  16.         } catch (UnsupportedEncodingException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         System.out.println(";编码:" + encodingName);  
  20.     }  
  21.   
  22.     public static void main(String[] args) {  
  23.         String en = "A";  
  24.         String ch = "人";  
  25.   
  26.         // 计算一个英文字母在各种编码下的字节数  
  27.         System.out.println("英文字母:" + en);  
  28.         EncodeTest.printByteLength(en, "GB2312");  
  29.         EncodeTest.printByteLength(en, "GBK");  
  30.         EncodeTest.printByteLength(en, "GB18030");  
  31.         EncodeTest.printByteLength(en, "ISO-8859-1");  
  32.         EncodeTest.printByteLength(en, "UTF-8");  
  33.         EncodeTest.printByteLength(en, "UTF-16");  
  34.         EncodeTest.printByteLength(en, "UTF-16BE");  
  35.         EncodeTest.printByteLength(en, "UTF-16LE");  
  36.   
  37.         System.out.println();  
  38.   
  39.         // 计算一个中文汉字在各种编码下的字节数  
  40.         System.out.println("中文汉字:" + ch);  
  41.         EncodeTest.printByteLength(ch, "GB2312");  
  42.         EncodeTest.printByteLength(ch, "GBK");  
  43.         EncodeTest.printByteLength(ch, "GB18030");  
  44.         EncodeTest.printByteLength(ch, "ISO-8859-1");  
  45.         EncodeTest.printByteLength(ch, "UTF-8");  
  46.         EncodeTest.printByteLength(ch, "UTF-16");  
  47.         EncodeTest.printByteLength(ch, "UTF-16BE");  
  48.         EncodeTest.printByteLength(ch, "UTF-16LE");  
  49.     }  
  50. }  

 

运行结果如下:

  1. 英文字母:A
  2. 字节数:1;编码:GB2312
  3. 字节数:1;编码:GBK
  4. 字节数:1;编码:GB18030
  5. 字节数:1;编码:ISO-8859-1
  6. 字节数:1;编码:UTF-8
  7. 字节数:4;编码:UTF-16
  8. 字节数:2;编码:UTF-16BE
  9. 字节数:2;编码:UTF-16LE
  10. 中文汉字:人
  11. 字节数:2;编码:GB2312
  12. 字节数:2;编码:GBK
  13. 字节数:2;编码:GB18030
  14. 字节数:1;编码:ISO-8859-1
  15. 字节数:3;编码:UTF-8
  16. 字节数:4;编码:UTF-16
  17. 字节数:2;编码:UTF-16BE
  18. 字节数:2;编码:UTF-16LE

可知,GB2312、GBK、GB18030三种编码格式都符合题目要求

 

如何判断哪个字符是中文,哪个是字母,可能有很多种方法,仁者见仁吧

一种,可以将字符串转化为字符数组,分别检查字符的GBK形式的字节长度

另一种,可以按照指定的字节数截取对应长度的字符串,然后判断子串的字节长度是否等于指定截取的字节长度,等于的话,说明子串没有中文,不等于的话,说明有中文字符。

请看相关代码:

Java代码  收藏代码
  1. /**   
  2.    * 判断是否是一个中文汉字   
  3.    *    
  4.     * @param c   
  5.    *            字符   
  6.     * @return true表示是中文汉字,false表示是英文字母   
  7.     * @throws UnsupportedEncodingException   
  8.     *             使用了JAVA不支持的编码格式   
  9.     */    
  10.    public static boolean isChineseChar(char c)     
  11.            throws UnsupportedEncodingException {     
  12.       // 如果字节数大于1,是汉字     
  13.       // 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了     
  14.       return String.valueOf(c).getBytes("GBK").length > 1;     
  15.    }     

 

Java代码  收藏代码
  1. /** 
  2.     * 将给定的字符串按着给定的截取长度截取 
  3.     * <br> 
  4.     * 注意一个汉字占2个字节 
  5.     * @param str 
  6.     * @param subSLength 
  7.     * @return 截取后的字符串 
  8.     * @throws UnsupportedEncodingException  
  9.     */  
  10.    public static String subStr(String str, int subSLength)  
  11.            throws UnsupportedEncodingException  
  12.    {  
  13.          
  14.        if (str == null)  
  15.            return null;  
  16.        else  
  17.        {  
  18.            int tempSubLength = subSLength;//截取字节数  
  19.              
  20.            String subStr = str.substring(0, subSLength);//截取的子串  
  21.              
  22.            int subStrByetsL = subStr.getBytes("GBK").length;//截取子串的字节长度  
  23.              
  24.            // 说明截取的字符串中包含有汉字  
  25.            while (subStrByetsL > tempSubLength)  
  26.            {  
  27.                subStr = str.substring(0, --subSLength);  
  28.                subStrByetsL = subStr.getBytes("GBK").length;  
  29.            }  
  30.            return subStr;  
  31.        }  
  32.          
  33.    }  
posted @ 2011-10-07 16:42  萧风的风  阅读(272)  评论(0编辑  收藏  举报