| package charset; |
| import java.io.UnsupportedEncodingException; |
| |
| |
| public class ChangeCharset { |
| |
| |
| public static final String US_ASCII = "US-ASCII"; |
| |
| public static final String ISO_8859_1 = "ISO-8859-1"; |
| |
| public static final String UTF_8 = "UTF-8"; |
| |
| public static final String UTF_16BE = "UTF-16BE"; |
| |
| public static final String UTF_16LE = "UTF-16LE"; |
| |
| public static final String UTF_16 = "UTF-16"; |
| |
| public static final String GBK = "GBK"; |
| |
| public static final String GB2312 = "GB2312"; |
| |
| |
| public String toASCII(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str, US_ASCII); |
| } |
| |
| |
| public String toISO_8859_1(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str, ISO_8859_1); |
| } |
| |
| |
| public String toUTF_8(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str, UTF_8); |
| } |
| |
| |
| public String toUTF_16BE(String str) throws UnsupportedEncodingException{ |
| return this.changeCharset(str, UTF_16BE); |
| } |
| |
| |
| public String toUTF_16LE(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str, UTF_16LE); |
| } |
| |
| |
| public String toUTF_16(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str, UTF_16); |
| } |
| |
| |
| public String toGBK(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str, GBK); |
| } |
| |
| |
| public String toGB2312(String str) throws UnsupportedEncodingException { |
| return this.changeCharset(str,GB2312); |
| } |
| |
| |
| |
| |
| |
| |
| public String changeCharset(String str, String newCharset) throws UnsupportedEncodingException { |
| if(str != null) { |
| |
| byte[] bs = str.getBytes(); |
| return new String(bs, newCharset); |
| } |
| return null; |
| } |
| |
| |
| |
| |
| |
| |
| |
| 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 String getURLEncoderString(String str) { |
| String result = ""; |
| if (null == str) { |
| return ""; |
| } |
| try { |
| result = java.net.URLEncoder.encode(str, "gb2312"); |
| } catch (UnsupportedEncodingException e) { |
| e.printStackTrace(); |
| } |
| return result; |
| } |
| |
| public static String URLDecoderString(String str) { |
| String result = ""; |
| if (null == str) { |
| return ""; |
| } |
| try { |
| result = java.net.URLDecoder.decode(str, "UTF-8"); |
| } catch (UnsupportedEncodingException e) { |
| e.printStackTrace(); |
| } |
| return result; |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理