?? stringutils.java
字號:
package com.yhcms.utils;
import javax.servlet.http.HttpServletRequest;
/**
* <p>Title:字符串轉換</p>
* <li>UBB代碼,html及字符串的轉換</li>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: www.yyhweb.com</p>
* @author stephen
*
*/
public class StringUtils {
/**
* @param request 當前請求
* @return 獲取該請求IP
*/
public static String getIP(HttpServletRequest request)
{
String ip = request.getRemoteAddr();
return ip != null ? ip : "";
}
/**
* @param str 要分割的字符串
* @param sp 分割字符
* @return 分割后的字符數組
*/
public static String[] split(String str, String sp)
{
if(str == null || str.equals(""))
return new String[0];
else
return str.split(sp);
}
/**
* @param str 要轉換的字符串
* @return 轉換后的字符串
*/
public static String htmlEncode(String str) {
if (str == null) {
str = "";
return str;
}
str = str.trim(); //?????β???
//str = replace(str, "<", "<");
//str = replace(str, ">", ">");
str = replace(str, "\t", " ");
str = replace(str, "\r\n", "\n");
str = replace(str, "\n", "<br>");
str = replace(str, " ", " ");
//str = replace(str, "'", "'");
//str = replace(str, "\\", "/");
str = replace(str, "\"",""");
return str;
}
/**
* @param str 要轉換的字符串
* @return 轉換后的字符串
*/
public static String strEncode(String str) {
if (str == null) {
str = "";
return str;
}
str = str.trim(); //?????β???
str = replace(str, "<", "<");
str = replace(str, ">", ">");
str = replace(str, "\t", " ");
str = replace(str, "\r\n", "\n");
str = replace(str, "\n", "<br>");
str = replace(str, " ", " ");
str = replace(str, "'", "'");
str = replace(str, "\\", "/");
str = replace(str, "\"",""");
return str;
}
/**
* @param str 含有UBB代碼的字符串
* @return 轉換后的字符串
*/
public static String ubbEncode(String str){
str = replaceB(str);
str = replaceI(str);
str = replaceU(str);
str = replaceCen(str);
str = replaceFly(str);
str = replaceMove(str);
str = replaceF(str);
str = replaceS(str);
str = replaceC(str);
str = replaceURL(str);
str = replaceIMG(str);
str = replaceEMAIL(str);
str = replaceCode(str);
str = replaceQuote(str);
str = replaceFLASH(str);
str = replaceRM(str);
str = replaceMP(str);
str = replaceQQ(str);
str = replaceFILE(str);
return str;
}
// [B]轉換為html
private static String replaceB(String str){
str = replace(str, "[B]", "<b>");
str = replace(str, "[/B]", "</b>");
return str;
}
// [I]轉換為html
private static String replaceI(String str){
str = replace(str, "[I]", "<i>");
str = replace(str, "[/I]", "</i>");
return str;
}
// [U]轉換為html
private static String replaceU(String str){
str = replace(str, "[U]", "<u>");
str = replace(str, "[/U]", "</u>");
return str;
}
// [center]轉換為html
private static String replaceCen(String str){
str = replace(str, "[center]", "<div align=center>");
str = replace(str, "[/center]", "</div>");
return str;
}
// [fly]轉換為html
private static String replaceFly(String str){
str = replace(str, "[fly]", "<marquee width=90% behavior=alternate scrollamount=3>");
str = replace(str, "[/fly]", "</marquee>");
return str;
}
// [move]轉換為html
private static String replaceMove(String str){
str = replace(str, "[move]", "<marquee width=90% scrollamount=3>");
str = replace(str, "[/move]", "</marquee>");
return str;
}
// [code]轉換為html
private static String replaceCode(String str){
str = replace(str, "[code]", "<table class=table_all cellpadding=3 cellspacing=1 border=0 align=center><tr><td class=tr_bg2 >");
str = replace(str, "[/code]", "</td></tr></table><br>");
return str;
}
// [quote]轉換為html
private static String replaceQuote(String str){
str = replace(str, "[quote]", "<table class=table_all cellpadding=3 cellspacing=1 border=0 align=center><tr><td class=tr_bg2 >");
str = replace(str, "[/quote]", "</td></tr></table><br>");
return str;
}
// [font]轉換為html
private static String replaceF(String str){
int strHead = str.indexOf("[font=");
int strTail = str.indexOf("[/font]");
while((strHead!=-1) && (strTail!=-1)){
int strTailLength = strTail+7;
String temp = str.substring(strHead+6,strTail);
int strHeadRight = temp.indexOf("]");
String tempStr = temp.substring(0,strHeadRight);
String text = temp.substring(strHeadRight+1,temp.length());
str = str.substring(0,strHead)+"<font face=\""+tempStr+"\">"+text+"</font>"+str.substring(strTailLength,str.length());
strHead = str.indexOf("[font=",strTailLength);
strTail = str.indexOf("[/font]",strTailLength);
}
return str;
// [size]??轉換為html??С
private static String replaceS(String str){
int strHead = str.indexOf("[size=");
int strTail = str.indexOf("[/size]");
while((strHead!=-1) && (strTail!=-1)){
int strTailLength = strTail+7;
String temp = str.substring(strHead+6,strTail);
int strHeadRight = temp.indexOf("]");
String tempStr = temp.substring(0,strHeadRight);
String text = temp.substring(strHeadRight+1,temp.length());
str = str.substring(0,strHead)+"<font size=\""+tempStr+"\">"+text+"</font>"+str.substring(strTailLength,str.length());
strHead = str.indexOf("[size=",strTailLength);
strTail = str.indexOf("[/size]",strTailLength);
}
return str;
}
// [color]轉換為html??С
private static String replaceC(String str){
int strHead = str.indexOf("[color=");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -