栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

某公司java面试题的解决方法

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

编程题 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我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.     public static void printByteLength(String s, String encodingName) {  
  6.         System.out.print(“字节数:”);  
  7.         try {  
  8.             System.out.print(s.getBytes(encodingName).length);  
  9.         } catch (UnsupportedEncodingException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.         System.out.println(“;编码:” + encodingName);  
  13.     }  
  14.   
  15.     public static void main(String[] args) {  
  16.         String en = “A”;  
  17.         String ch = “人”;  
  18.   
  19.         // 计算一个英文字母在各种编码下的字节数  
  20.         System.out.println(“英文字母:” + en);  
  21.         EncodeTest.printByteLength(en, “GB2312”);  
  22.         EncodeTest.printByteLength(en, “GBK”);  
  23.         EncodeTest.printByteLength(en, “GB18030”);  
  24.         EncodeTest.printByteLength(en, “ISO-8859-1”);  
  25.         EncodeTest.printByteLength(en, “UTF-8”);  
  26.         EncodeTest.printByteLength(en, “UTF-16”);  
  27.         EncodeTest.printByteLength(en, “UTF-16BE”);  
  28.         EncodeTest.printByteLength(en, “UTF-16LE”);  
  29.   
  30.         System.out.println();  
  31.   
  32.         // 计算一个中文汉字在各种编码下的字节数  
  33.         System.out.println(“中文汉字:” + ch);  
  34.         EncodeTest.printByteLength(ch, “GB2312”);  
  35.         EncodeTest.printByteLength(ch, “GBK”);  
  36.         EncodeTest.printByteLength(ch, “GB18030”);  
  37.         EncodeTest.printByteLength(ch, “ISO-8859-1”);  
  38.         EncodeTest.printByteLength(ch, “UTF-8”);  
  39.         EncodeTest.printByteLength(ch, “UTF-16”);  
  40.         EncodeTest.printByteLength(ch, “UTF-16BE”);  
  41.         EncodeTest.printByteLength(ch, “UTF-16LE”);  
  42.     }  
  43. }  

 

运行结果如下:

  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.    public static boolean isChineseChar(char c)     
  3.            throws UnsupportedEncodingException {     
  4.       // 如果字节数大于1,是汉字     
  5.       // 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了     
  6.       return String.valueOf(c).getBytes(“GBK”).length > 1;     
  7.    }     

 

Java代码 
 

收藏代码

  1.   
  2.    public static String subStr(String str, int subSLength)  
  3.            throws UnsupportedEncodingException  
  4.    {  
  5.          
  6.        if (str == null)  
  7.            return null;  
  8.        else  
  9.        {  
  10.            int tempSubLength = subSLength;//截取字节数  
  11.              
  12.            String subStr = str.substring(0, subSLength);//截取的子串  
  13.              
  14.            int subStrByetsL = subStr.getBytes(“GBK”).length;//截取子串的字节长度  
  15.              
  16.            // 说明截取的字符串中包含有汉字  
  17.            while (subStrByetsL > tempSubLength)  
  18.            {  
  19.                subStr = str.substring(0, –subSLength);  
  20.                subStrByetsL = subStr.getBytes(“GBK”).length;  
  21.            }  
  22.            return subStr;  
  23.        }  
  24.          
  25.    }  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/263651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号