java字符串转成utf-8_将字符串的编码格式转换为utf-8-程序员宅基地

技术标签: java字符串转成utf-8  

方式一:

/**

* 将字符串的编码格式转换为utf-8

*

* @param str

* @return Name = new

* String(Name.getBytes("ISO-8859-1"), "utf-8");

*/

public static String toUTF8(String str) {

if (isEmpty(str)) {

return "";

}

try {

if (str.equals(new String(str.getBytes("GB2312"), "GB2312"))) {

str = new String(str.getBytes("GB2312"), "utf-8");

return str;

}

} catch (Exception exception) {

}

try {

if (str.equals(new String(str.getBytes("ISO-8859-1"), "ISO-8859-1"))) {

str = new String(str.getBytes("ISO-8859-1"), "utf-8");

return str;

}

} catch (Exception exception1) {

}

try {

if (str.equals(new String(str.getBytes("GBK"), "GBK"))) {

str = new String(str.getBytes("GBK"), "utf-8");

return str;

}

} catch (Exception exception3) {

}

return str;

}

/**

* 判断是否为空

*

* @param str

* @return

*/

public static boolean isEmpty(String str) {

// 如果字符串不为null,去除空格后值不与空字符串相等的话,证明字符串有实质性的内容

if (str != null && !str.trim().isEmpty()) {

return false;// 不为空

}

return true;// 为空

}

方式二:

import java.io.UnsupportedEncodingException;

/**

* 转换字符串的编码

*/

public class ChangeCharset {

/** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */

public static final String US_ASCII = "US-ASCII";

/** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */

public static final String ISO_8859_1 = "ISO-8859-1";

/** 8 位 UCS 转换格式 */

public static final String UTF_8 = "UTF-8";

/** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */

public static final String UTF_16BE = "UTF-16BE";

/** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */

public static final String UTF_16LE = "UTF-16LE";

/** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */

public static final String UTF_16 = "UTF-16";

/** 中文超大字符集 */

public static final String GBK = "GBK";

/**

* 将字符编码转换成US-ASCII码

*/

public String toASCII(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, US_ASCII);

}

/**

* 将字符编码转换成ISO-8859-1码

*/

public String toISO_8859_1(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, ISO_8859_1);

}

/**

* 将字符编码转换成UTF-8码

*/

public String toUTF_8(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_8);

}

/**

* 将字符编码转换成UTF-16BE码

*/

public String toUTF_16BE(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_16BE);

}

/**

* 将字符编码转换成UTF-16LE码

*/

public String toUTF_16LE(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_16LE);

}

/**

* 将字符编码转换成UTF-16码

*/

public String toUTF_16(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_16);

}

/**

* 将字符编码转换成GBK码

*/

public String toGBK(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, GBK);

}

/**

* 字符串编码转换的实现方法

* @param str 待转换编码的字符串

* @param newCharset 目标编码

* @return

* @throws UnsupportedEncodingException

*/

public String changeCharset(String str, String newCharset)

throws UnsupportedEncodingException {

if (str != null) {

//用默认字符编码解码字符串。

byte[] bs = str.getBytes();

//用新的字符编码生成字符串

return new String(bs, newCharset);

}

return null;

}

/**

* 字符串编码转换的实现方法

* @param str 待转换编码的字符串

* @param oldCharset 原编码

* @param newCharset 目标编码

* @return

* @throws UnsupportedEncodingException

*/

public String changeCharset(String str, String oldCharset, String newCharset)

throws UnsupportedEncodingException {

if (str != null) {

//用旧的字符编码解码字符串。解码可能会出现异常。

byte[] bs = str.getBytes(oldCharset);

//用新的字符编码生成字符串

return new String(bs, newCharset);

}

return null;

}

public static void main(String[] args) throws UnsupportedEncodingException {

ChangeCharset test = new ChangeCharset();

String str = "This is a 中文的 String!";

System.out.println("str: " + str);

String gbk = test.toGBK(str);

System.out.println("转换成GBK码: " + gbk);

System.out.println();

String ascii = test.toASCII(str);

System.out.println("转换成US-ASCII码: " + ascii);

gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK);

System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk);

System.out.println();

String iso88591 = test.toISO_8859_1(str);

System.out.println("转换成ISO-8859-1码: " + iso88591);

gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK);

System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk);

System.out.println();

String utf8 = test.toUTF_8(str);

System.out.println("转换成UTF-8码: " + utf8);

gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK);

System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk);

System.out.println();

String utf16be = test.toUTF_16BE(str);

System.out.println("转换成UTF-16BE码:" + utf16be);

gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK);

System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk);

System.out.println();

String utf16le = test.toUTF_16LE(str);

System.out.println("转换成UTF-16LE码:" + utf16le);

gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK);

System.out.println("再把UTF-16LE码的字符串转换成GBK码: " + gbk);

System.out.println();

String utf16 = test.toUTF_16(str);

System.out.println("转换成UTF-16码:" + utf16);

gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK);

System.out.println("再把UTF-16码的字符串转换成GBK码: " + gbk);

String s = new String("中文".getBytes("UTF-8"),"UTF-8");

System.out.println(s);

}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_36038435/article/details/114088729

智能推荐

传奇假人自动上线_传奇商业脚本 各种M2防假人脚本大集合 传奇私服脚本-程序员宅基地

文章浏览阅读4.9k次。各种M2防假人脚本大集合BLUE的M2防假人攻击脚本:首先先在D:\MirServer\Mir200\Envir\QuestDiary\数据文件 目录下建一个“激活.txt”文本文挡其次在D:\MirServer\Mir200\Envir\MapQuest_def 目录下的QManage.txt脚本里加上如下一段[@Login]#ifchecknamelist ..\QuestDiary\数据文件..._传奇防挂机流星验证

系统工程的本质_为什么要强调系统工程-程序员宅基地

文章浏览阅读2.6k次,点赞3次,收藏15次。原文链接:https://mp.weixin.qq.com/s/9uTG1QyjOzIfeMuvioNkWw 工程,是日常生活和工作时经常使用的一个词语,例如土木工程、电子工程、机械工程、系统工程,以及法制工程、希望工程、菜篮子工程;也有三峡工程、载人航天工程、探月工程等;还有“创新是一项系统工程”“改革是一项系统工程”等说法。这个词语本身并没有令人费解的地方,但在不同的语境中,却体现出迥..._为什么要强调系统工程

TensorFlow里,shape=(?, 120)里的问号是怎么回事呢_tf shape为什么有问号-程序员宅基地

文章浏览阅读4.1k次。TensorFlow里,shape=(?, 120)里的问号是怎么回事呢125self.cos_sim(1024,51)Tensor("cosine-imilarity-layer/mul_2:0",shape=(1024,51),dtype=float32)126type(self.query_batch):<class'tensorflow...._tf shape为什么有问号

[RK3568 Android11] 开发之调试RK809 codec音频-SPK喇叭_rk3568 喇叭左右声道问题-程序员宅基地

文章浏览阅读5.5k次,点赞2次,收藏10次。目录前言一、注册声卡顺序二、SPK喇叭路由配置三、tinyalsa调试工具前言瑞芯微RK809是一种复杂的功率管理集成电路(PMIC)集成编解码器,适用于由外部电源供电的多核系统应用;同时它也集成了音频编解码器,这样在硬件设计上可以直接使用RK809 codec功能,不必再外挂个其他codec芯片;RK809 codec不仅支持SPK,还支持耳机,MIC功能;一、注册声卡顺序设备树dts里面原厂帮我们配置好了rk809_sound,我们直接..._rk3568 喇叭左右声道问题

验证码的两种生成方式_"<img id=\"code\" src=\"verify/code.action\">"-程序员宅基地

文章浏览阅读1.8k次。验证码是一个非常常见的东西,基本上每一个做web开发的程序员都会遇到。本文介绍两种验证码的生成方式。一种是后台生成验证码,一种是前台直接生成验证码在spring MVC模式 中,后台生成生成验证码传递到前台。后台代码:import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImag_""

VMware16安装CentOS7、网络配置、远程连接_远程访问vm16虚拟机-程序员宅基地

文章浏览阅读1.3k次,点赞2次,收藏8次。目录1、安装准备2、安装3、网络配置4、远程连接安装准备VMware16转载 - Linux>>CentOS 7镜像下载安装转载 - VMware 16 安装 CentOS 7.9 详细图文教程注意1、以下不要选,我选了启动不了虚拟机2、若在安装时配置网络,网关、掩码参数需要确定网络配置:连通网络,设置静态IP初始状态:ping不通百度、IP地址为空、网卡配置文件也是默认配置方法方法一:安装时配置,如上述教程方法二:安装后进入系统配置1、_远程访问vm16虚拟机

随便推点

杰里之.V006 音箱版本删除录音文件,需要优化 system.a 库【篇】_ac6965a电路图-程序员宅基地

文章浏览阅读139次。// folder : /xxxxxx // filename : xxxx0000.yyy u8 file_api_delete_file(const char *path, const char *folder, const char *filename, u32 *file_index)_ac6965a电路图

教大家如何去看开源项目_开源项目哪里找-程序员宅基地

文章浏览阅读2.2k次,点赞2次,收藏10次。如何去看开源项目_开源项目哪里找

Linux 硬件时间(RTC time)、系统时间(UTC时间、Universal time)、本地时间(Local time)、时区(Time zone)与夏令时(DST)解析-程序员宅基地

文章浏览阅读8.3k次,点赞8次,收藏34次。硬件时间,也被称为实时时钟(RTC),是指计算机主板上的一个独立于操作系统的设备,它在电源关闭甚至断电情况下也能保持运行。其功能是记录当前的日期和时间。系统时间是计算机内部使用的时间,它通常在启动时从RTC设置,然后由系统时钟进行跟踪。系统时钟是操作系统内核的一部分,可以以毫秒或纳秒级别提供精确时间。本地时间是系统时间经过时区转换后的时间。时区是根据地理位置确定的,全球分为24个时区,每个时区大约代表15度的经度。例如,北京时间是UTC+8,而伦敦时间是UTC+0。时区是为了方便统一时间而划分的地理区域。_rtc time

免费获取股票历史交易数据方法与代码获取股票实时数据方法集合_哪里有股票的历史实时数据-程序员宅基地

文章浏览阅读6.7k次,点赞2次,收藏34次。小编在这里提供2种方法去获取股票数据,第一种通过使用第三方平台提供的方法来获取到所需要的股票历史数据。第二种通过接口使用代码获取实时的股票数据_哪里有股票的历史实时数据

泛微Ecology9.0流程Ecode实践:通过修改Store对象隐藏流程明细表列实例_泛微oa根据主表选择按钮隐藏明细表列-程序员宅基地

文章浏览阅读570次,点赞7次,收藏9次。Ecology9复写组件,E9隐藏明细表列,E9隐藏明细表栏,泛微隐藏明细表列_泛微oa根据主表选择按钮隐藏明细表列

趋势预测算法大PK!_趋势预测算法 csdn-程序员宅基地

文章浏览阅读1w次,点赞5次,收藏61次。https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/106368395趋势预测在很多应用场景中都会起到至关重要的作用,比如淘宝商家会考虑库存量应该保持在多少才能够满足客户需求,商场希望得知假期会迎来多大的客流量以安排系列活动,机场想要预测五一黄金周会有多大的客运量来做相应的应急部署等。在智能运维领域,趋势预测同样具有一定的理论意义和实际应用价值。趋势预测在运维场景中的应用背景在实时监控系统中会采集到大量的数据,有些数据具有周期性等时_趋势预测算法 csdn

推荐文章

热门文章

相关标签