⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutility.java

📁 本源码为教学管理信息系统
💻 JAVA
字号:
package com.wygl.xmlhttp;/** * 字符串工具类,根据用户提供的原始字符集和目标字符集,将字符串转换成目标字符集的字符串 * <p>Title:StringUtility </p> * <p>Description:字符串工具类  </p> * <p>Copyright: Copyright (c) 2004</p> * @version 1.0 */public class StringUtility {   	/**   * 转码方法   * @param plainText String 需要转码的字符串   * @throws Exception   * @return String   */  	private static String encode(String plainText) throws Exception {	  	if (plainText==null){	  		return "";	  	}	  	String s = "";	  	    	StringBuffer encodedText = new StringBuffer();        for(int i = 0; i < plainText.length(); i++){            int iChar = plainText.charAt(i);            if(iChar > 255){                s = Integer.toString(iChar, 16);                for(int j = s.length(); j < 4; j++)                    s = "0" + s;                encodedText.append("#" + s);            } else if(iChar < 48 || iChar > 57 && iChar < 65 || iChar > 90 && iChar < 97 || iChar > 122){                s = Integer.toString(iChar, 16);                for(int j = s.length(); j < 2; j++)                    s = "0" + s;                encodedText.append("~" + s);            } else {                encodedText.append(plainText.charAt(i));            }        }        return encodedText.toString();   	}  	/**   * 解码方法   * @param encodedText String 需要解码的字符串   * @throws Exception   * @return String   */	private static String decode(String encodedText) throws Exception {        if(encodedText == null)            return "";        String s = "";        StringBuffer plainText = new StringBuffer();        for(int i = 0; i < encodedText.length(); i++){            char c = encodedText.charAt(i);            switch(c) {	            case 126: // '~'	                s = encodedText.substring(i + 1, i + 3);	                plainText.append((char)Integer.parseInt(s, 16));	                i += 2;	                break;		            case 35: // '#'	                s = encodedText.substring(i + 1, i + 5);	                plainText.append((char)Integer.parseInt(s, 16));	                i += 4;	                break;		            default:	                plainText.append(c);	                break;            }        }        return plainText.toString();    } 	/**   * 获得转码方法   * @param str String 需要转码的字符串   * @throws Exception   * @return String   */ 	public static String getEncodeStr(String str) throws Exception{		try {			return encode(str);		} catch (Exception e) {			throw new Exception("转码出错,"+e.getMessage());		} 	} 	 /**   * 获得解码方法   * @param str String 需要解码的字符串   * @throws Exception   * @return String   */  	 public static String getDecodeStr(String str) throws Exception{		try {			return decode(str);		} catch (Exception e) {			throw new Exception("反转码出错,"+e.getMessage());		} 	} 	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -