`
hooray520
  • 浏览: 112827 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

判断字符串编码

    博客分类:
  • JAVA
 
阅读更多

猜测法:猜测一种字符串编码,然后使用该编码对字符串进行编码,还原。如果猜测错误,字符串会被破坏,还原城乱码。


/**
	 * 判断字符串编码
	 * 
	 * @param str
	 * @return
	 */
	public static String getEncoding (String str)
	{

		String encode = "GB2312";
		try
		{
			if (str.equals (new String (str.getBytes (encode), encode)))
			{
				String s = encode;
				return s;
			}
		}
		catch (Exception exception)
		{
		}
		encode = "ISO-8859-1";
		try
		{
			if (str.equals (new String (str.getBytes (encode), encode)))
			{
				String s1 = encode;
				return s1;
			}
		}
		catch (Exception exception1)
		{
		}
		encode = "UTF-8";
		try
		{
			if (str.equals (new String (str.getBytes (encode), encode)))
			{
				String s2 = encode;
				return s2;
			}
		}
		catch (Exception exception2)
		{
		}
		encode = "GBK";
		try
		{
			if (str.equals (new String (str.getBytes (encode), encode)))
			{
				String s3 = encode;
				return s3;
			}
		}
		catch (Exception exception3)
		{
		}
		return "";
	}  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics