jsp 中文转化及字符转化BEAN(代码)

jsp 中文转化及字符转化BEAN(代码)
package com.yangzhou.sql; 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*; 
public class Data { 
private static char _hexChars[] = {
'0','1','2','3','4','5','6','7',
'8','9','a','b','c','d','e','f'
}; 
/**
* 替换用户输入字符串中的非法字符
* @param input 输入字符串
* @return 返回经过转换后字符串
*/
public String escapeHTMLTages(String input){
//如果输入字符串为空,直接返回
if(input.trim()==null||input.length() == 0){
return input;
} 
//建立一个StringBuffer来处理输入数据
StringBuffer buf = new StringBuffer(input.length() + 6);
char ch=' '; 
//依次将输入字符串中的非法字符替换掉
for(int i=0;i<input.length();i++){
ch = input.charAt(i);
if(ch=='<'){
buf.append("<");
}
else if (ch=='>'){
buf.append(">");
}
else if (ch=='\n'){
buf.append("<br>");
}
else if (ch==' '){
buf.append(" ");
}
else if (ch=='\''){
buf.append("''");
}
else if (ch=='\"'){
buf.append(""");
}
else{
buf.append(ch);
}
}
return buf.toString();
}
/**
* 转化为HTML代码
* @param strInput 输入字符串
* @return 返回经过转换后字符串
*/
public String HTMLTages(String strInput){
String strOutput;
String result;
int flagHtml;
do{
flagHtml = strInput.indexOf("<br>");
if (flagHtml != -1){
result = strInput.substring(0,flagHtml);
result = result + "\n";
result = result +strInput.substring(flagHtml + 4);
strInput = result;
}
}while (flagHtml != -1);
return strInput.toString();
}
/**
* 返回经过中文转换后字符串
* @param str 输入字符串
* @throws Exception 字符转换异常
* @return 返回经过中文转换后字符串
*/
public String getStr(String str) throws Exception
{
try{
String temp_p = str;
if(temp_p!=null) {
byte[] temp_t = temp_p.getBytes("ISO8859-1");
String temp = new String(temp_t);
return temp;
}else
return "null";
}catch(Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* 替换用户输入字符串中的非法字符(不转换<br>)
* @param input 输入字符串
* @return 返回经过转换后字符串
*/
public String escapeHTMLTages1(String input){
//如果输入字符串为空,直接返回
if(input.trim()==null||input.length() == 0){
return input;
} 
//建立一个StringBuffer来处理输入数据
StringBuffer buf = new StringBuffer(input.length() + 6);
char ch=' '; 
//依次将输入字符串中的非法字符替换掉
for(int i=0;i<input.length();i++){
ch = input.charAt(i);
if(ch=='<'){
buf.append("<");
}
else if (ch=='>'){
buf.append(">");
}
else if (ch==' '){
buf.append(" ");
}
else if (ch=='\''){
buf.append("''");
}
else{
buf.append(ch);
}
}
return buf.toString();
}
/**
* 替换空字符串
* @param s 输入字符串
* @return 返回经过转换后字符串
*/
public String escapeNull(String s){
//如果输入字符串为空,直接返回
if(s==null||s==""){
return "空";
}else
return s;
}
/**
*sha 算法
*@param bytes 输入字符串
*@return 返回加密后字符串
*/
public static String GetAsHexaDecimal(byte[] bytes) {
StringBuffer s = new StringBuffer(bytes.length*2);
int length = bytes.length;
for (int n=0; n < length; n++) {
int number = bytes[n];
number = (number < 0) ? (-number + 127) : number; // shift to positive range
// 0 <-> 255
int sixteens = number / 16;
int remainder = number - (sixteens * 16);
s.append(_hexChars[sixteens]);
s.append(_hexChars[remainder]);
}
return s.toString();
}
public static String GetAsHexaDecimal(String str) {
StringBuffer s = new StringBuffer(str.length()*2);
int length = str.length();
for (int n=0; n < length; n++) {
int number = str.charAt(n);
number = (number < 0) ? (-number + 127) : number; // shift to positive range
// 0 <-> 255
int sixteens = number / 16;
int remainder = number - (sixteens * 16);
s.append(_hexChars[sixteens]);
s.append(_hexChars[remainder]);
}
return s.toString();
}
/**
* @param i1 数组大小
* @param s1 输入字符串
* @param s2 查询字符串
* @return String[] 提取字符数组
*/
public String[] distillString(int i1,String s1,String s2) {
String[] strArray = new String[i1];
int startIndex = 0;
int endIndex = 0;
for(int i=0;i<i1;i++) {
startIndex = s1.indexOf("|",startIndex);
if(startIndex!=-1) {
endIndex = s1.indexOf("|",startIndex+1);
if(endIndex!=-1) {
strArray[i] = s1.substring(startIndex+1,endIndex);
startIndex = endIndex;
}
}
}
return strArray;
}
/**
* @param i1 数组大小
* @param s1 输入字符串
* @return String[] 提取字符数组
*/
public Vector distillString(String s1) {
Vector v = new Vector();
int startIndex = 0;
int endIndex = 0;
do {
startIndex = s1.indexOf("|",startIndex);
if(startIndex!=-1) {
endIndex = s1.indexOf("|",startIndex+1);
if(endIndex!=-1) {
v.addElement(s1.substring(startIndex+1,endIndex));
startIndex = endIndex;
}
}
}while(startIndex!=-1&&endIndex!=-1);
return v;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值