zoukankan      html  css  js  c++  java
  • 面试题:字符串截取

    字符串截取

    题目:在java中,字符串“abcd”与字符串“ab你好”的长度是一样。都是四个字符。

    但相应的字节数不同。一个汉字占两个字节。定义一个方法,依照指定的字节数来取子串。

    如:

    对于“ab你好”,假设取三个字节,那么子串就是ab与“你”字的半个。那么半个就要舍弃。

    假设取四个字节就是“ab你”,取五个字节还是“ab你”。

    解题思路:咋一看,以为就是注意汉字字节时的处理问题。事实上还应该考虑在不同的编码格式中,汉字占的字节也会不一样,如:gbk编码格式下汉字一般占两个字节,且

    均小于0,而在utf-8,汉字占两个字节,可是是一负一正。所以要考虑系统当前编码格式。

    代码:

    
    import java.io.UnsupportedEncodingException;
    
    public class StringCutDemo {
    
    	public static void main(String[] args) {
    		String str="ab你好琲c琲琲我琲琲琲琲";//大部分汉字的字节码值:2个负数,另一小部分不经常使用的汉字如“琲”的字节码值是:1负1正(前负后正)
    		byte buf[] = null;
    //		try {
    //			//buf = str.getBytes("gbk");
    //			buf = str.getBytes("utf-8");
    //			
    //		} catch (UnsupportedEncodingException e) {
    //			e.printStackTrace();
    //		}
    		buf = str.getBytes();
    		
    		//观察
    		for(byte b:buf){
    			System.out.print(b+" ");
    		}
    		System.out.println();
    		
    		//測试
    		System.out.println(str);
    		System.out.println("---------------------");
    		for(int i=0;i<=buf.length;i++){
    			//String s = cutStringByByteGbk(str,i);//当为gbk格式时
    			//String s = cutStringByByteUtf8(str,i);//当为utf-8时2
    //			String s = cutStringByByte(str,i);//推断系统编码格式,然后调用上面两个方法
    			String s = cutString(str,i);//确定当前格式。然后通用两种格式
    			System.out.println("截取"+i+"个,结果是:"+s);
    		}
    		
    	}
    
    	private static String cutString(String str, int len) {
    		try {
    			//获取字节数组
    			String string=System.getProperty("file.encoding");
    			int a=0;
    			if(string.equalsIgnoreCase("gbk")){
    				a=2;
    			}
    			if(string.equalsIgnoreCase("utf-8")){
    				a=3;
    			}
    			if(a==0){
    				return "";
    			}
    			byte buf[] = str.getBytes(string);
    			int count=0;
    			//从第len个字节处開始,从后往前,统计字节码值为负的“字节的个数”
    			for(int i=len-1; i>=0; i--){
    				if(buf[i]<0){
    					count++;
    				}else{
    					break;
    				}
    			}
    			if(count%a==0){//字节码值为负的字节的个数为偶数。则说明汉字刚好截整齐
    				return new String(buf,0,len,string);
    			}else{//奇数,要丢掉多余的字节
    				return new String(buf,0,len-count%a,string);
    			}
    		} catch (UnsupportedEncodingException e) {
    			throw new RuntimeException("字符串编码异常!");
    		}
    	}
    
    	private static String cutStringByByteGbk(String str, int len) {
    		try {
    			//获取字节数组
    			byte buf[] = str.getBytes("GBK");
    			int count=0;
    			//从第len个字节处開始,从后往前。统计字节码值为负的“字节的个数”
    			for(int i=len-1; i>=0; i--){
    				if(buf[i]<0){
    					count++;
    				}else{
    					break;
    				}
    			}
    			if(count%2==0){//字节码值为负的字节的个数为偶数。则说明汉字刚好截整齐
    				return new String(buf,0,len,"gbk");
    			}else{//奇数,要丢掉最后一个字节
    				return new String(buf,0,len-1,"gbk");
    			}
    		} catch (UnsupportedEncodingException e) {
    			throw new RuntimeException("字符串编码异常!");
    		}
    		
    	}
    	
    	
    	private static String cutStringByByteUtf8(String str, int len) {
    		try {
    			//获取字节数组
    			byte buf[] = str.getBytes("utf-8");
    			int count=0;
    			//从第len个字节处開始,从后往前。统计字节码值为负的“字节的个数”
    			for(int i=len-1; i>=0; i--){
    				if(buf[i]<0){
    					count++;
    				}else{
    					break;
    				}
    			}
    			if(count%3==0){//字节码值为负的字节的个数为3的倍数,则说明汉字刚好截整齐
    				return new String(buf,0,len,"utf-8");
    			}else{//其他,要丢掉最后多余的字节
    				return new String(buf,0,len-count%3,"utf-8");
    			}
    		} catch (UnsupportedEncodingException e) {
    			throw new RuntimeException("字符串编码异常!");
    		}
    	}
    
    	public static String cutStringByByte(String str, int len){
    		if(System.getProperty("file.encoding").equalsIgnoreCase("gbk")){
    //			System.out.println(System.getProperty("file.encoding"));
    			return cutStringByByteGbk(str,len);
    		}
    		if(System.getProperty("file.encoding").equalsIgnoreCase("utf-8")){
    			return cutStringByByteUtf8(str,len);
    		}
    		return "";
    	}
    	
    }
    
    
    
    






  • 相关阅读:
    Sql server 备份还原后出现“受限制用户”问题
    jQuery的deferred对象详解
    SQLServer 自增主键创建, 指定自增主键列值插入数据,插入主键
    无法添加来自此网站的应用 扩展程序
    C#中dynamic的正确用法
    C#编程总结(十四)dynamic
    基于params,ref,out的参数问题详解
    SQLServer 数据库查看死锁、堵塞的SQL语句
    如何对web.config进行加密和解密
    自定义HttpModule的一些经验--配置篇
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7065971.html
Copyright © 2011-2022 走看看